<!DOCTYPE html><html lang="zh-CN"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1"><meta name="format-detection" content="telephone=no"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><link rel="icon" href="/blog/blog/images/icons/favicon-16x16.png?v=2.6.2" type="image/png" sizes="16x16"><link rel="icon" href="/blog/blog/images/icons/favicon-32x32.png?v=2.6.2" type="image/png" sizes="32x32"><meta name="description" content="1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）                           一、求累加       12345678910111213141516int main()&amp;#123;				int i &#x3D;0;		int res &#x3D; 0;		int n;		cin &gt;&gt; n;				while(i&lt;">
<meta property="og:type" content="article">
<meta property="og:title" content="C&#x2F;C++ 编程常见代码">
<meta property="og:url" content="https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%20%E7%BC%96%E7%A8%8B%E5%B8%B8%E8%A7%81%E4%BB%A3%E7%A0%81/index.html">
<meta property="og:site_name" content="斯人若彩虹 遇上方知有">
<meta property="og:description" content="1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）                           一、求累加       12345678910111213141516int main()&amp;#123;				int i &#x3D;0;		int res &#x3D; 0;		int n;		cin &gt;&gt; n;				while(i&lt;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210116103426548.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210116105046701.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210120142711373.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210202123009154.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210206145605924.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20210206151824180.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70">
<meta property="article:published_time" content="2021-03-11T09:51:27.000Z">
<meta property="article:modified_time" content="2021-05-06T11:45:16.000Z">
<meta property="article:author" content="何水浪">
<meta property="article:tag" content="C语言">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20210116103426548.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70"><title>C/C++ 编程常见代码 | 斯人若彩虹 遇上方知有</title><link ref="canonical" href="https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%20%E7%BC%96%E7%A8%8B%E5%B8%B8%E8%A7%81%E4%BB%A3%E7%A0%81/"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.12.1/css/all.min.css" type="text/css"><link rel="stylesheet" href="/blog/css/index.css?v=2.6.2"><script>var Stun = window.Stun || {};
var CONFIG = {
  root: '/blog/',
  algolia: undefined,
  assistSearch: undefined,
  fontIcon: {"prompt":{"success":"fas fa-check-circle","info":"fas fa-arrow-circle-right","warning":"fas fa-exclamation-circle","error":"fas fa-times-circle"},"copyBtn":"fas fa-copy"},
  sidebar: {"offsetTop":"20px","tocMaxDepth":6},
  header: {"enable":true,"showOnPost":true,"scrollDownIcon":false},
  postWidget: {"endText":true},
  nightMode: {"enable":true},
  back2top: {"enable":true},
  codeblock: {"style":"default","highlight":"light","wordWrap":false},
  reward: false,
  fancybox: false,
  zoomImage: {"gapAside":"20px"},
  galleryWaterfall: undefined,
  lazyload: false,
  pjax: undefined,
  externalLink: {"icon":{"enable":true,"name":"fas fa-external-link-alt"}},
  shortcuts: undefined,
  prompt: {"copyButton":"复制","copySuccess":"复制成功","copyError":"复制失败"},
  sourcePath: {"js":"js","css":"css","images":"images"},
};

window.CONFIG = CONFIG;</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/blog/atom.xml" title="斯人若彩虹 遇上方知有" type="application/atom+xml">
</head><body><div class="container" id="container"><header class="header" id="header"><div class="header-inner"><nav class="header-nav header-nav--fixed"><div class="header-nav-inner"><div class="header-nav-menubtn"><i class="fas fa-bars"></i></div><div class="header-nav-menu"><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/"><span class="header-nav-menu-item__icon"><i class="fas fa-home"></i></span><span class="header-nav-menu-item__text">首页</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/archives/"><span class="header-nav-menu-item__icon"><i class="fas fa-folder-open"></i></span><span class="header-nav-menu-item__text">归档</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/categories/"><span class="header-nav-menu-item__icon"><i class="fas fa-layer-group"></i></span><span class="header-nav-menu-item__text">分类</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/tags/"><span class="header-nav-menu-item__icon"><i class="fas fa-tags"></i></span><span class="header-nav-menu-item__text">标签</span></a></div><div class="header-nav-menu-item"><a class="header-nav-menu-item__link" href="/blog/friends/"><span class="header-nav-menu-item__icon"><i class="fas fa-link"></i></span><span class="header-nav-menu-item__text">友链</span></a></div></div><div class="header-nav-search"><span class="header-nav-search__icon"><i class="fas fa-search"></i></span><span class="header-nav-search__text">搜索</span></div><div class="header-nav-mode"><div class="mode"><div class="mode-track"><span class="mode-track-moon"></span><span class="mode-track-sun"></span></div><div class="mode-thumb"></div></div></div></div></nav><div class="header-banner"><div class="header-banner-info"><div class="header-banner-info__title">斯人若彩虹 遇上方知有</div><div class="header-banner-info__subtitle"></div></div></div></div></header><main class="main" id="main"><div class="main-inner"><div class="content-wrap" id="content-wrap"><div class="content" id="content"><!-- Just used to judge whether it is an article page--><div id="is-post"></div><div class="post"><header class="post-header"><h1 class="post-title">C/C++ 编程常见代码</h1><div class="post-meta"><span class="post-meta-item post-meta-item--createtime"><span class="post-meta-item__icon"><i class="far fa-calendar-plus"></i></span><span class="post-meta-item__info">发表于</span><span class="post-meta-item__value">2021-03-11</span></span><span class="post-meta-item post-meta-item--updatetime"><span class="post-meta-item__icon"><i class="far fa-calendar-check"></i></span><span class="post-meta-item__info">更新于</span><span class="post-meta-item__value">2021-05-06</span></span><span class="post-meta-item post-meta-item--wordcount"><span class="post-meta-item__icon"><i class="far fa-file-word"></i></span><span class="post-meta-item__info">字数统计</span><span class="post-meta-item__value">7.8k</span></span><span class="post-meta-item post-meta-item--readtime"><span class="post-meta-item__icon"><i class="far fa-clock"></i></span><span class="post-meta-item__info">阅读时长</span><span class="post-meta-item__value">76分</span></span></div></header><div class="post-body">
        <h2 id="1-加法器、累加器：（多实现-1-2-…-n-以及阶乘-n-的功能）"   >
          <a href="#1-加法器、累加器：（多实现-1-2-…-n-以及阶乘-n-的功能）" class="heading-link"><i class="fas fa-link"></i></a><a href="#1-加法器、累加器：（多实现-1-2-…-n-以及阶乘-n-的功能）" class="headerlink" title="1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）"></a>1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）</h2>
      
        <h4 id="一、求累加"   >
          <a href="#一、求累加" class="heading-link"><i class="fas fa-link"></i></a><a href="#一、求累加" class="headerlink" title="一、求累加"></a>一、求累加</h4>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">int</span> i =<span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> res = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">int</span> n;</span><br><span class="line">		cin &gt;&gt; n;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(i&lt;=n)&#123;	</span><br><span class="line">			res += i;</span><br><span class="line">			i++;	</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//其中 res为最终结果 n表示加到多少 i表示每次加的数 </span></span><br><span class="line">		</span><br><span class="line">		cout &lt;&lt; res &lt;&lt;endl;</span><br><span class="line">		<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">	&#125;</span><br></pre></td></tr></table></div></figure>

        <h4 id="二、求某数阶乘"   >
          <a href="#二、求某数阶乘" class="heading-link"><i class="fas fa-link"></i></a><a href="#二、求某数阶乘" class="headerlink" title="二、求某数阶乘"></a>二、求某数阶乘</h4>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">int</span> sum = <span class="number">1</span>; </span><br><span class="line">	<span class="keyword">int</span> n;</span><br><span class="line">	cin &gt;&gt; n;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(i &lt;=n)&#123;</span><br><span class="line">		</span><br><span class="line">		sum *=i;    <span class="comment">// 1. sum = 1*1 =1</span></span><br><span class="line">		i++;		<span class="comment">// 2. i =2;</span></span><br><span class="line">					<span class="comment">// 3. sum = 1*2=2 以此类推 </span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;其阶乘为：&quot;</span> &lt;&lt; sum &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="2-求两个数或数组中的最大值与最小值"   >
          <a href="#2-求两个数或数组中的最大值与最小值" class="heading-link"><i class="fas fa-link"></i></a><a href="#2-求两个数或数组中的最大值与最小值" class="headerlink" title="2. 求两个数或数组中的最大值与最小值"></a>2. 求两个数或数组中的最大值与最小值</h2>
      <p>int main(){</p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">	<span class="keyword">int</span> a, b,max;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请连续输入待比较的两个数，空格隔开：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	max = (a&gt;b) ? a : b; <span class="comment">//使用双目运算符取得最大值</span></span><br><span class="line">	cout &lt;&lt; max &lt;&lt;endl;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">int</span> arr[<span class="number">5</span>] = &#123;<span class="number">5</span>, <span class="number">2</span>, <span class="number">13</span>, <span class="number">23</span>, <span class="number">66</span>&#125;;</span><br><span class="line">	<span class="keyword">int</span> arr_max, arr_min;</span><br><span class="line">	arr_max = arr[<span class="number">0</span>]; <span class="comment">//默认第一位元素为最值 </span></span><br><span class="line">	arr_min = arr[<span class="number">0</span>];</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i &lt;<span class="number">5</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">			<span class="keyword">if</span>(arr_max &lt; arr[i])</span><br><span class="line">			&#123;</span><br><span class="line">				arr_max = arr[i];		</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(arr_min &gt; arr[i])&#123;  <span class="comment">//往后遍历，若还有更大的则更新最值 否则最小值更新</span></span><br><span class="line">				arr_min = arr[i]; </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; arr_min &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt;arr_max &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="3-冒泡排序，必须会默写"   >
          <a href="#3-冒泡排序，必须会默写" class="heading-link"><i class="fas fa-link"></i></a><a href="#3-冒泡排序，必须会默写" class="headerlink" title="3.冒泡排序，必须会默写"></a>3.冒泡排序，必须会默写</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LEN 5</span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[LEN] = &#123;<span class="number">2</span>, <span class="number">13</span>, <span class="number">5</span>, <span class="number">55</span>, <span class="number">20</span>&#125;;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;LEN; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;LEN-i<span class="number">-1</span>; j++)&#123;</span><br><span class="line">		 <span class="comment">//注意两个for循环的初始条件与终止条件 都从0k开始</span></span><br><span class="line">			<span class="keyword">if</span>(a[j] &gt; a[j+<span class="number">1</span>])&#123;</span><br><span class="line">				<span class="keyword">int</span>  t = a[j];</span><br><span class="line">				a[j] = a[j+<span class="number">1</span>];</span><br><span class="line">				a[j+<span class="number">1</span>] = t;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="4-简单的switch-case多分支条件语句"   >
          <a href="#4-简单的switch-case多分支条件语句" class="heading-link"><i class="fas fa-link"></i></a><a href="#4-简单的switch-case多分支条件语句" class="headerlink" title="4.简单的switch-case多分支条件语句"></a>4.简单的switch-case多分支条件语句</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> score;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请在0~100范围内输入你的分数(满分100分)：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot; 60分以下不及格 60~70及格 70~80良好 80以上优秀  &quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; score;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in"><span class="keyword">switch</span></span>(score/<span class="number">10</span>)&#123;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">6</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;及格&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">7</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;良好&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">8</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;优秀&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">9</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;优秀&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">case</span> <span class="number">10</span> :</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;满分&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		<span class="keyword">default</span>:</span><br><span class="line">			cout&lt;&lt;<span class="string">&quot;不及格&quot;</span>&lt;&lt;endl; </span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="5-求两个数的最大公约数、最小公倍数-不可含0-谭浩强C语言例题）"   >
          <a href="#5-求两个数的最大公约数、最小公倍数-不可含0-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#5-求两个数的最大公约数、最小公倍数-不可含0-谭浩强C语言例题）" class="headerlink" title="5.求两个数的最大公约数、最小公倍数(不可含0) (谭浩强C语言例题）"></a>5.求两个数的最大公约数、最小公倍数(不可含0) (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> x, y, r;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入待运算的两个数, 中间用空格隔开&quot;</span> &lt;&lt;endl; </span><br><span class="line">	cin &gt;&gt; x &gt;&gt; y;</span><br><span class="line">	<span class="keyword">int</span> mul = x * y;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(x &gt; y) <span class="comment">//将比如 8,2 与 2,8 的操作统一输出</span></span><br><span class="line">	&#123;</span><br><span class="line">		r = x;</span><br><span class="line">		x = y;</span><br><span class="line">		y = r;  </span><br><span class="line">	 &#125; </span><br><span class="line">	 </span><br><span class="line">	r = x % y;  <span class="comment">//直接判断两数是否可以整除 </span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(r)&#123;</span><br><span class="line">		x = y;</span><br><span class="line">		y = r;</span><br><span class="line">		r = x % y; </span><br><span class="line">		<span class="comment">//辗转相除法</span></span><br><span class="line">		<span class="comment">//直到余数不为0 </span></span><br><span class="line">		<span class="comment">//将余数作为除数 较小者作为被除数 继续求余判断</span></span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;最大公约数:&quot;</span> &lt;&lt; y &lt;&lt; <span class="string">&quot;，最小公倍数:&quot;</span> &lt;&lt; mul / y &lt;&lt;endl;	 </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="6-斐波那契数列递归"   >
          <a href="#6-斐波那契数列递归" class="heading-link"><i class="fas fa-link"></i></a><a href="#6-斐波那契数列递归" class="headerlink" title="6.斐波那契数列递归"></a>6.斐波那契数列递归</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fib</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(n == <span class="number">1</span>)&#123;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;<span class="keyword">else</span> <span class="keyword">if</span>(n == <span class="number">2</span>)&#123;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="number">1</span>;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line"></span><br><span class="line">		<span class="keyword">return</span> <span class="built_in">fib</span>(n<span class="number">-2</span>) + <span class="built_in">fib</span>(n<span class="number">-1</span>);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>
<p>1, 1, 2, 3, 5…以此类推</p>

        <h2 id="7-判断某数是否是素数-谭浩强C语言例题）"   >
          <a href="#7-判断某数是否是素数-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#7-判断某数是否是素数-谭浩强C语言例题）" class="headerlink" title="7.判断某数是否是素数 (谭浩强C语言例题）"></a>7.判断某数是否是素数 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">int</span> n, i;</span><br><span class="line">  <span class="keyword">bool</span> isPrime = <span class="literal">true</span>;</span><br><span class="line"> </span><br><span class="line">  cout &lt;&lt; <span class="string">&quot;输入一个正整数: &quot;</span>;</span><br><span class="line">  cin &gt;&gt; n;</span><br><span class="line"> </span><br><span class="line">  <span class="keyword">for</span>(i = <span class="number">2</span>; i &lt;= n / <span class="number">2</span>; ++i)</span><br><span class="line">  &#123;</span><br><span class="line">      <span class="keyword">if</span>(n % i == <span class="number">0</span>) <span class="comment">//只能被1 和 自身整除的数 才能是素数 </span></span><br><span class="line">      &#123;</span><br><span class="line">          isPrime = <span class="literal">false</span>;</span><br><span class="line">          <span class="keyword">break</span>;</span><br><span class="line">      &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">if</span> (isPrime)</span><br><span class="line">      cout &lt;&lt; <span class="string">&quot;是素数&quot;</span>;</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">      cout &lt;&lt; <span class="string">&quot;不是素数&quot;</span>;</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="8-大写字母转化为小写字母"   >
          <a href="#8-大写字母转化为小写字母" class="heading-link"><i class="fas fa-link"></i></a><a href="#8-大写字母转化为小写字母" class="headerlink" title="8.大写字母转化为小写字母"></a>8.大写字母转化为小写字母</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">char</span> a;</span><br><span class="line">	a = <span class="built_in">getchar</span>();</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, a+<span class="number">32</span>); <span class="comment">// C或C语言中字符数据均以ASCII值存储 大写字母与小写字母相差32</span></span><br><span class="line">	</span><br><span class="line">  <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="9-统计输入字符中单词的个数-谭浩强C语言例题）"   >
          <a href="#9-统计输入字符中单词的个数-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#9-统计输入字符中单词的个数-谭浩强C语言例题）" class="headerlink" title="9.统计输入字符中单词的个数 (谭浩强C语言例题）"></a>9.统计输入字符中单词的个数 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 30</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> b[N];</span><br><span class="line">	<span class="keyword">int</span> word,count=<span class="number">0</span>,i; <span class="comment">//word代表是否是单词 1代表是 0代表不是单词 而是空格 </span></span><br><span class="line">						<span class="comment">//count 代表单词个数	 </span></span><br><span class="line">	<span class="built_in">puts</span>(<span class="string">&quot;输入的字符长度要小于等于N&quot;</span>); <span class="comment">// 例如-&gt;  hello world </span></span><br><span class="line">	<span class="built_in">gets</span>(b);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(i=<span class="number">0</span>;b[i]!=<span class="string">&#x27;\0&#x27;</span>;i++)</span><br><span class="line">		<span class="keyword">if</span>(b[i]==<span class="string">&#x27; &#x27;</span>)  <span class="comment">//若为空格则不是单词 </span></span><br><span class="line">		   word=<span class="number">0</span>; </span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(word==<span class="number">0</span>) <span class="comment">//若是空格则遇到的新单词使得count加一 </span></span><br><span class="line">		&#123;</span><br><span class="line">			count++;</span><br><span class="line">			word=<span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d&quot;</span>,count);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="10-求Sn-a-aa-aaa-…-aa…aaa（有n个a）之值，其中a是一个数字，为2。-例如，n-5时-2-22-222-2222-22222，n由键盘输入。-谭浩强C语言例题）"   >
          <a href="#10-求Sn-a-aa-aaa-…-aa…aaa（有n个a）之值，其中a是一个数字，为2。-例如，n-5时-2-22-222-2222-22222，n由键盘输入。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#10-求Sn-a-aa-aaa-…-aa…aaa（有n个a）之值，其中a是一个数字，为2。-例如，n-5时-2-22-222-2222-22222，n由键盘输入。-谭浩强C语言例题）" class="headerlink" title="10.求Sn=a+aa+aaa+…+aa…aaa（有n个a）之值，其中a是一个数字，为2。 例如，n=5时=2+22+222+2222+22222，n由键盘输入。 (谭浩强C语言例题）"></a>10.求Sn=a+aa+aaa+…+aa…aaa（有n个a）之值，其中a是一个数字，为2。 例如，n=5时=2+22+222+2222+22222，n由键盘输入。 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> n,i,Sn =<span class="number">0</span>,a=<span class="number">0</span>;</span><br><span class="line">    cin &gt;&gt; n;  <span class="comment">//n代表循环次数</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span>(i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">    	a = a*<span class="number">10</span> +<span class="number">2</span>;   <span class="comment">// 0, 2, 22, 222 </span></span><br><span class="line">    	Sn += a;		/<span class="number">0</span>+<span class="number">2</span>+<span class="number">22</span> </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; Sn;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="11-求Sn-1-2-3-4-5-…-n-之值，其中n是一个数字-n不超过20-注意使用int类型-当n为19时会溢出-谭浩强C语言例题）"   >
          <a href="#11-求Sn-1-2-3-4-5-…-n-之值，其中n是一个数字-n不超过20-注意使用int类型-当n为19时会溢出-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#11-求Sn-1-2-3-4-5-…-n-之值，其中n是一个数字-n不超过20-注意使用int类型-当n为19时会溢出-谭浩强C语言例题）" class="headerlink" title="11.求Sn=1!+2!+3!+4!+5!+…+n!之值，其中n是一个数字(n不超过20) 注意使用int类型 当n为19时会溢出 (谭浩强C语言例题）"></a>11.求Sn=1!+2!+3!+4!+5!+…+n!之值，其中n是一个数字(n不超过20) 注意使用int类型 当n为19时会溢出 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">long</span> <span class="keyword">long</span> <span class="title">fac</span><span class="params">(<span class="keyword">long</span> <span class="keyword">long</span> n)</span></span>&#123;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> i = <span class="number">1</span>;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> sum = <span class="number">1</span>; </span><br><span class="line">	<span class="keyword">while</span>(i &lt;=n)&#123;</span><br><span class="line">		sum *=i;    <span class="comment">// 1. sum = 1*1 =1</span></span><br><span class="line">		i++;		<span class="comment">// 2. i =2;</span></span><br><span class="line">					<span class="comment">// 3. sum = 1*2=2 以此类推 </span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">long</span> <span class="keyword">long</span> n,sum=<span class="number">0</span>,a = <span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; n;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">long</span> <span class="keyword">long</span> i=n; i&gt;<span class="number">0</span>; i--)&#123;</span><br><span class="line">		sum += <span class="built_in">fac</span>(i); </span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; sum &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="12-求一个3×3矩阵对角线元素之和。-谭浩强C语言例题）"   >
          <a href="#12-求一个3×3矩阵对角线元素之和。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#12-求一个3×3矩阵对角线元素之和。-谭浩强C语言例题）" class="headerlink" title="12.求一个3×3矩阵对角线元素之和。 (谭浩强C语言例题）"></a>12.求一个3×3矩阵对角线元素之和。 (谭浩强C语言例题）</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210116103426548.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> a[<span class="number">3</span>][<span class="number">3</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">3</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;<span class="number">3</span>; j++)&#123;</span><br><span class="line">			cin &gt;&gt; a[i][j];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;  <span class="comment">//两个for循环输入二维数组</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> sum1 = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> sum2 = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">3</span>;i++)</span><br><span class="line">	&#123;</span><br><span class="line">	  sum1 += a[i][i]; <span class="comment">//主对角线求和</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> j=<span class="number">2</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;<span class="number">3</span>;i++)</span><br><span class="line">	 &#123; </span><br><span class="line">	 	</span><br><span class="line">	 	sum2 += a[i][j]; </span><br><span class="line">	 	<span class="comment">//副对角线求和 注意变量j定义在循环外 不然值不会发生改变</span></span><br><span class="line">		<span class="comment">//j分别是2 1 0 </span></span><br><span class="line">		 j--;</span><br><span class="line">	 	</span><br><span class="line">	 &#125;</span><br><span class="line">	cout &lt;&lt; sum1 &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; sum2;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="13-用选择法对10个整数从小到大排序。-谭浩强C语言例题）"   >
          <a href="#13-用选择法对10个整数从小到大排序。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#13-用选择法对10个整数从小到大排序。-谭浩强C语言例题）" class="headerlink" title="13.用选择法对10个整数从小到大排序。 (谭浩强C语言例题）"></a>13.用选择法对10个整数从小到大排序。 (谭浩强C语言例题）</h2>
      <p> <img   src="https://img-blog.csdnimg.cn/20210116105046701.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> LEN 10</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> *a,<span class="keyword">int</span> *b)</span>  <span class="comment">//交换两个数 引用&amp; 或者指针* 均可</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> temp = *a;</span><br><span class="line">    *a = *b;</span><br><span class="line">    *b = temp;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">selection_sort</span><span class="params">(<span class="keyword">int</span> arr[], <span class="keyword">int</span> len)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> i,j;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span> ; i &lt; len - <span class="number">1</span> ; i++)</span><br><span class="line">    &#123;</span><br><span class="line">                <span class="keyword">int</span> min = i; <span class="comment">//默认选择最小值 </span></span><br><span class="line">                <span class="keyword">for</span> (j = i + <span class="number">1</span>; j &lt; len; j++)    </span><br><span class="line">                        <span class="keyword">if</span> (arr[j] &lt; arr[min])    </span><br><span class="line">                                min = j;    		</span><br><span class="line">                <span class="built_in">swap</span>(&amp;arr[min], &amp;arr[i]);    </span><br><span class="line">                <span class="comment">//调用数组元素时 元素前加引用 </span></span><br><span class="line">        &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> a[LEN];</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;LEN; i++)</span><br><span class="line">		cin &gt;&gt; a[i];</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">selection_sort</span>(a, LEN);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;LEN; i++)&#123;</span><br><span class="line">		cout &lt;&lt; a[i] &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="14-数组插入处理，对一个排好序的数组，按原来的规律插入某数"   >
          <a href="#14-数组插入处理，对一个排好序的数组，按原来的规律插入某数" class="heading-link"><i class="fas fa-link"></i></a><a href="#14-数组插入处理，对一个排好序的数组，按原来的规律插入某数" class="headerlink" title="14.数组插入处理，对一个排好序的数组，按原来的规律插入某数"></a>14.数组插入处理，对一个排好序的数组，按原来的规律插入某数</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210120142711373.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[<span class="number">5</span>],x;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">		<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span> ,&amp;a[i]); <span class="comment">//输入数组</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d&quot;</span>, &amp;x); <span class="comment">//输入待插入元素</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;<span class="number">5</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(a[i]&lt;x &amp;&amp; a[i+<span class="number">1</span>]&gt;x)  <span class="comment">//满足条件模拟“插入” 并输入前后元素 </span></span><br><span class="line">		cout &lt;&lt; a[i] &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt;x &lt;&lt;<span class="string">&quot; &quot;</span>;</span><br><span class="line">		<span class="keyword">else</span> </span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d &quot;</span>, a[i]); <span class="comment">//否则正常输入</span></span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="15-写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。-谭浩强C语言例题）"   >
          <a href="#15-写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#15-写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。-谭浩强C语言例题）" class="headerlink" title="15.写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。 (谭浩强C语言例题）"></a>15.写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 3 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[N][N];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;N;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;N;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			cin &gt;&gt; a[i][j];</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>;i&lt;N;i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>;j&lt;N;j++)</span><br><span class="line">		&#123;</span><br><span class="line">			cout &lt;&lt; a[j][i] &lt;&lt;<span class="string">&quot; &quot;</span>; <span class="comment">//行列互换即可</span></span><br><span class="line">		&#125;</span><br><span class="line">		cout &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="16-自定义函数之字符串反转"   >
          <a href="#16-自定义函数之字符串反转" class="heading-link"><i class="fas fa-link"></i></a><a href="#16-自定义函数之字符串反转" class="headerlink" title="16.自定义函数之字符串反转"></a>16.自定义函数之字符串反转</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> a[N];</span><br><span class="line">	cin &gt;&gt; a; </span><br><span class="line">	<span class="comment">//遇tab、空格、回车都结束 </span></span><br><span class="line">	<span class="comment">//适合输入单个字符串且中间不含空格 </span></span><br><span class="line">	<span class="comment">//若需输入含空格的字符串 可用gets(str);</span></span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(a);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=len<span class="number">-1</span>; i&gt;=<span class="number">0</span>; i--)&#123; </span><br><span class="line">	<span class="comment">//注意字符数组的最后一个字符是 &#x27;\0&#x27;因而从倒数第二个开始</span></span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, a[i]);</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="17-写一函数，将两个字符串连接"   >
          <a href="#17-写一函数，将两个字符串连接" class="heading-link"><i class="fas fa-link"></i></a><a href="#17-写一函数，将两个字符串连接" class="headerlink" title="17.写一函数，将两个字符串连接"></a>17.写一函数，将两个字符串连接</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">char</span> a[N], b[N];</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; a;</span><br><span class="line">	cin &gt;&gt; b;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> len_a = <span class="built_in">strlen</span>(a);</span><br><span class="line">	<span class="keyword">int</span> len_b = <span class="built_in">strlen</span>(b);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> j = len_a,k=<span class="number">0</span>; j &lt; len_a + len_b; j++,k++ )</span><br><span class="line">	&#123;</span><br><span class="line">		a[j] = b[k]; <span class="comment">//后半部分赋新值即可</span></span><br><span class="line">	&#125;	</span><br><span class="line">	cout &lt;&lt; a &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="18-写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1-9-9-0”。-谭浩强C语言例题）"   >
          <a href="#18-写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1-9-9-0”。-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#18-写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1-9-9-0”。-谭浩强C语言例题）" class="headerlink" title="18.写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1 9 9 0”。 (谭浩强C语言例题）"></a>18.写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1 9 9 0”。 (谭浩强C语言例题）</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//1990	</span></span><br><span class="line">	<span class="keyword">int</span> a;</span><br><span class="line">	stack&lt;<span class="keyword">int</span>&gt; s; <span class="comment">//利用栈stack从个位到最高位 进栈</span></span><br><span class="line">	cin &gt;&gt; a;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(a&gt;<span class="number">0</span>)&#123;</span><br><span class="line">		s.<span class="built_in">push</span>(a%<span class="number">10</span>);</span><br><span class="line">		a /= <span class="number">10</span>;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">	<span class="keyword">while</span>(!s.<span class="built_in">empty</span>())</span><br><span class="line">	&#123;</span><br><span class="line">		cout &lt;&lt; s.<span class="built_in">top</span>() &lt;&lt; <span class="string">&quot; &quot;</span>; <span class="comment">//根据栈的FILO原则 出栈即可</span></span><br><span class="line">		s.<span class="built_in">pop</span>();	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="19-定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。"   >
          <a href="#19-定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。" class="heading-link"><i class="fas fa-link"></i></a><a href="#19-定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。" class="headerlink" title="19.定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。"></a>19.定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> exchange(a,b) t=a;a=b;b=t;  <span class="comment">//相当于整段代码替换 </span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a, b, t=<span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">exchange</span>(a,b);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; a &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; b &lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="20-输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。"   >
          <a href="#20-输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。" class="heading-link"><i class="fas fa-link"></i></a><a href="#20-输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。" class="headerlink" title="20.输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。"></a>20.输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> MOD(a,b,res) res = a % b;  <span class="comment">//相当于整段代码替换 </span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a, b, res=<span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">MOD</span>(a,b,res);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; res &lt;&lt;endl;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="21-输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’a’其它字符不变）。"   >
          <a href="#21-输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’a’其它字符不变）。" class="heading-link"><i class="fas fa-link"></i></a><a href="#21-输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’a’其它字符不变）。" class="headerlink" title="21.输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’ａ’其它字符不变）。"></a>21.输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’ａ’其它字符不变）。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> a[N];</span><br><span class="line">	<span class="built_in">gets</span>(a);  <span class="comment">//输入字符串</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; a[i] != <span class="string">&#x27;\0&#x27;</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(a[i] &gt;= <span class="string">&#x27;a&#x27;</span> &amp;&amp; a[i] &lt; <span class="string">&#x27;z&#x27;</span>|| a[i] &gt;= <span class="string">&#x27;A&#x27;</span> &amp;&amp; a[i] &lt; <span class="string">&#x27;Z&#x27;</span>)&#123;</span><br><span class="line">			a[i] += <span class="number">1</span>; </span><br><span class="line">			<span class="comment">//C++与C一样 字符变量都由ASCII码整数存储</span></span><br><span class="line">			<span class="comment">//因而 相应ASCII码自增1即可</span></span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(a[i]==<span class="string">&#x27;z&#x27;</span>)</span><br><span class="line">			a[i] = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(a[i]==<span class="string">&#x27;Z&#x27;</span>)</span><br><span class="line">			a[i] = <span class="string">&#x27;A&#x27;</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="built_in">puts</span>(a); <span class="comment">//打印字符串</span></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>; </span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="22-现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。-编写一个函数input-用来输入一个学生的数据记录。-编写一个函数print-打印一个学生的数据记录。-在主函数调用这两个函数，读取N条记录输入，再按要求输出。-N-lt-100"   >
          <a href="#22-现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。-编写一个函数input-用来输入一个学生的数据记录。-编写一个函数print-打印一个学生的数据记录。-在主函数调用这两个函数，读取N条记录输入，再按要求输出。-N-lt-100" class="heading-link"><i class="fas fa-link"></i></a><a href="#22-现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。-编写一个函数input-用来输入一个学生的数据记录。-编写一个函数print-打印一个学生的数据记录。-在主函数调用这两个函数，读取N条记录输入，再按要求输出。-N-lt-100" class="headerlink" title="22.现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数，读取N条记录输入，再按要求输出。 N&lt;100"></a>22.现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数，读取N条记录输入，再按要求输出。 N&lt;100</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210202123009154.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//class:</span></span><br><span class="line"><span class="comment">//私有属性：学号，姓名，三科成绩</span></span><br><span class="line"><span class="comment">//公有方法：void input(); void print();</span></span><br><span class="line"> </span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Student</span>&#123;</span> <span class="comment">//类名可以不大写，但是推荐大写 符合Java命名规范</span></span><br><span class="line">	<span class="keyword">public</span>:</span><br><span class="line">		<span class="function"><span class="keyword">void</span> <span class="title">input</span><span class="params">()</span></span>;</span><br><span class="line">		<span class="function"><span class="keyword">void</span> <span class="title">print</span><span class="params">()</span></span>;</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">private</span>:</span><br><span class="line">		string num; </span><br><span class="line">		<span class="comment">//字符串类型 涉及到char 跟 int 的组合 </span></span><br><span class="line">		<span class="comment">//C++中的字符串是string类 Java中是String类</span></span><br><span class="line">		string name;</span><br><span class="line">		<span class="keyword">int</span> Chinese;</span><br><span class="line">		<span class="keyword">int</span> Math;</span><br><span class="line">		<span class="keyword">int</span> English;</span><br><span class="line">&#125;; <span class="comment">//记得有分号； </span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Student::input</span><span class="params">()</span></span>&#123;   <span class="comment">//记得域名符:: 表示实现Student类的方法 </span></span><br><span class="line">	cin &gt;&gt; num &gt;&gt; name &gt;&gt; Chinese &gt;&gt; Math &gt;&gt; English; <span class="comment">//加上域名符直接引用属性 </span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Student::print</span><span class="params">()</span></span>&#123;</span><br><span class="line">	cout &lt;&lt; num &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; name &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; Chinese &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; Math &lt;&lt; <span class="string">&quot;,&quot;</span> &lt;&lt; English &lt;&lt;endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> n;</span><br><span class="line">	Student stu[N];</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; n;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">		stu[i].<span class="built_in">input</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">		stu[i].<span class="built_in">print</span>();</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

        <h2 id="23-将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。"   >
          <a href="#23-将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。" class="heading-link"><i class="fas fa-link"></i></a><a href="#23-将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。" class="headerlink" title="23.将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。"></a>23.将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> str[N];</span><br><span class="line">	<span class="built_in">gets</span>(str);</span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(str);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=len<span class="number">-1</span>; i&gt;=<span class="number">0</span>; i--)&#123; </span><br><span class="line">	<span class="comment">//利用for循环颠倒该数组即可</span></span><br><span class="line">	<span class="comment">//由于C++中最后一个字符是&#x27;\0&#x27; 因此从倒数第二个字符开始自减</span></span><br><span class="line">		cout &lt;&lt; str[i];</span><br><span class="line">	&#125; </span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="24-解一元二次方程-谭浩强C语言例题）"   >
          <a href="#24-解一元二次方程-谭浩强C语言例题）" class="heading-link"><i class="fas fa-link"></i></a><a href="#24-解一元二次方程-谭浩强C语言例题）" class="headerlink" title="24.解一元二次方程 (谭浩强C语言例题）"></a>24.解一元二次方程 (谭浩强C语言例题）</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210206145605924.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"><span class="comment">//	a*x^2+b*x+c=0 </span></span><br><span class="line">	<span class="keyword">int</span> a, b, c;</span><br><span class="line">	<span class="keyword">double</span> res1, res2;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">double</span> delta = <span class="built_in">pow</span>(b,<span class="number">2</span>) - <span class="number">4</span> * a * c;</span><br><span class="line">	<span class="keyword">if</span>(delta == <span class="number">0</span>)&#123;</span><br><span class="line">		res1 = (-b + <span class="built_in">sqrt</span>(delta) ) / <span class="number">2</span> * a;</span><br><span class="line">		res2 = res1;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		res1 = (-b + <span class="built_in">sqrt</span>(delta) ) / <span class="number">2</span> * a;</span><br><span class="line">		res2 = (-b - <span class="built_in">sqrt</span>(delta) ) / <span class="number">2</span> * a;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%.2f %.2f&quot;</span>, res1, res2);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>


        <h2 id="25-输入字符串-将组成字符的所有非英文字母的字符删除后输出"   >
          <a href="#25-输入字符串-将组成字符的所有非英文字母的字符删除后输出" class="heading-link"><i class="fas fa-link"></i></a><a href="#25-输入字符串-将组成字符的所有非英文字母的字符删除后输出" class="headerlink" title="25.输入字符串 将组成字符的所有非英文字母的字符删除后输出"></a>25.输入字符串 将组成字符的所有非英文字母的字符删除后输出</h2>
      <p><img   src="https://img-blog.csdnimg.cn/20210206151824180.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70" style="width: image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,tepx;height: t_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L20wXzQzNDQ1MDY3,size_16,color_FFFFFF,t_70px;"  alt="在这里插入图片描述"></p>
<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//a ASCII是97</span></span><br><span class="line">	<span class="keyword">char</span> str[N]; <span class="comment">//定义所输入的字符数组</span></span><br><span class="line">	<span class="built_in">gets</span>(str); 	<span class="comment">//输入该数组的数据</span></span><br><span class="line">	<span class="keyword">int</span> len = <span class="built_in">strlen</span>(str);</span><br><span class="line">	</span><br><span class="line">	vector&lt;<span class="keyword">char</span>&gt; v; <span class="comment">//定义vector模板类</span></span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(str[i] &lt; <span class="string">&#x27;A&#x27;</span>|| str[i] &gt;<span class="string">&#x27;Z&#x27;</span>&amp;&amp; str[i] &lt;<span class="string">&#x27;a&#x27;</span> || str[i]&gt;<span class="string">&#x27;z&#x27;</span>)&#123;  </span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			<span class="comment">//所输入数据是大小写字母时 存入动态数组vector中</span></span><br><span class="line">			v.<span class="built_in">push_back</span>(str[i]);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i &lt; v.<span class="built_in">size</span>(); i++)&#123;</span><br><span class="line">		cout &lt;&lt; v[i];</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>



        <h2 id="谭浩强第四版课后习题答案："   >
          <a href="#谭浩强第四版课后习题答案：" class="heading-link"><i class="fas fa-link"></i></a><a href="#谭浩强第四版课后习题答案：" class="headerlink" title="谭浩强第四版课后习题答案："></a>谭浩强第四版课后习题答案：</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br></pre></td><td class="code"><pre><span class="line">### 第二章：</span><br><span class="line"><span class="number">2.4</span></span><br><span class="line">#<span class="number">1</span>、以参数形式交换两个数（记得参数加引用）</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> &amp;a, <span class="keyword">int</span> &amp;b)</span></span>&#123; </span><br><span class="line"><span class="comment">//什么时候用引用？ 当需要将操作后的值 传递回去</span></span><br><span class="line">	<span class="keyword">int</span> t = a;</span><br><span class="line">	a = b;</span><br><span class="line">	b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1, num2;</span><br><span class="line">	cin &gt;&gt; num1 &gt;&gt; num2;</span><br><span class="line">	<span class="built_in">swap</span>(num1, num2);</span><br><span class="line">	cout &lt;&lt; num1 &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; num2;	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">2.</span>依次输入<span class="number">5</span>个值 要求输入其中的最大值</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> L 5</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[L],max;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;L; i++)&#123;</span><br><span class="line">		cin &gt;&gt; a[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	max = a[<span class="number">0</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;=<span class="number">5</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(max &lt; a[i])</span><br><span class="line">			max = a[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;Max is:&quot;</span> &lt;&lt; max &lt;&lt; endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">3.</span>有三个数a,b,c 要求输出最大值</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> L 5</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1, num2, num3;</span><br><span class="line">	<span class="keyword">int</span> max;</span><br><span class="line">	cin &gt;&gt; num1 &gt;&gt; num2 &gt;&gt; num3;</span><br><span class="line">	max = (num1 &gt; num2)? num1 : num2; <span class="comment">//第一个数与第二个数比较 结果是max</span></span><br><span class="line">	max = (max &gt; num3)? max : num3;	  <span class="comment">//max与第三个数比较 重新赋max的值</span></span><br><span class="line">	cout &lt;&lt; max &lt;&lt;endl; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">4.</span>求<span class="number">1</span>+<span class="number">2</span>+<span class="number">3</span>+...+<span class="number">100</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> i, sum=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(i&lt;=<span class="number">100</span>)&#123;</span><br><span class="line">		sum += i;</span><br><span class="line">		i++;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; sum &lt;&lt;endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">5.</span>判断一个数是否同时被<span class="number">3</span>和<span class="number">5</span>整除</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num;</span><br><span class="line">	cin &gt;&gt; num;</span><br><span class="line">	<span class="keyword">if</span>(num%<span class="number">3</span>==<span class="number">0</span> &amp;&amp; num%<span class="number">5</span> ==<span class="number">0</span>)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;该数%d可以被3和5整除&quot;</span>, num);</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;不行&quot;</span>);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">6.</span>输出<span class="number">100</span>~<span class="number">200</span>之间的素数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">fac</span><span class="params">(<span class="keyword">int</span> x)</span></span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">true</span>;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">2</span>; i&lt;x/<span class="number">2</span>; i++)&#123; <span class="comment">//注意 对素数的判断 是需要判断2~n/2即可</span></span><br><span class="line">		<span class="keyword">if</span>(x%i==<span class="number">0</span>)&#123;</span><br><span class="line">			flag = <span class="literal">false</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">100</span>; i&lt;=<span class="number">200</span>; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">fac</span>(i) == <span class="literal">true</span>)</span><br><span class="line">		&#123;</span><br><span class="line">			<span class="built_in">printf</span>(<span class="string">&quot;%d是素数\n&quot;</span>, i);</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">7</span> 求两个数的最大公约数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> a, <span class="keyword">int</span> b)</span></span>&#123; <span class="comment">//传参进来a大于b </span></span><br><span class="line">	<span class="keyword">int</span> c = a%b; </span><br><span class="line">	<span class="comment">//辗转相除法 若余数为0 则小者为最大公约数</span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(c != <span class="number">0</span>)&#123; <span class="comment">//如果余数不为0 </span></span><br><span class="line">		a = b; <span class="comment">//否则将余数作为除数  </span></span><br><span class="line">		b = c; <span class="comment">//小者作为被除数</span></span><br><span class="line">		c = a % b;		</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; b &lt;&lt;<span class="string">&quot;是最大公约数&quot;</span> &lt;&lt; endl;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">int</span> &amp;a, <span class="keyword">int</span> &amp;b)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> t = a;</span><br><span class="line">	a = b;</span><br><span class="line">	b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num1, num2;</span><br><span class="line">	cin &gt;&gt; num1 &gt;&gt; num2;</span><br><span class="line">	<span class="keyword">if</span>(num1 &lt; num2)</span><br><span class="line">		<span class="built_in">swap</span>(num1, num2); <span class="comment">//对大小相反做处理 交换即可</span></span><br><span class="line">	<span class="built_in">fun</span>(num1, num2);	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">#<span class="number">8.</span> 求一元二次方程的根 </span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span> </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//ax^2 + bx + c</span></span><br><span class="line">	<span class="keyword">double</span> a, b, c;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b &gt;&gt; c;</span><br><span class="line">	<span class="keyword">double</span> delt = <span class="built_in">pow</span>(b,<span class="number">2</span>) - <span class="number">4</span> * a * c;</span><br><span class="line">	<span class="keyword">double</span> x1 = (-b+<span class="built_in">sqrt</span>(delt))/(<span class="number">2</span>*a);</span><br><span class="line">	<span class="keyword">double</span> x2 = (-b-<span class="built_in">sqrt</span>(delt))/(<span class="number">2</span>*a);</span><br><span class="line">		</span><br><span class="line">	<span class="keyword">if</span>(delt &gt; <span class="number">0</span>)&#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;实根分别为：&quot;</span> &lt;&lt; x1 &lt;&lt; <span class="string">&quot; &quot;</span> &lt;&lt; x2 &lt;&lt;endl;</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(delt == <span class="number">0</span>)&#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;实根为:&quot;</span> &lt;&lt; x1 &lt;&lt; endl;			</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;无实根&quot;</span> &lt;&lt; endl; </span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125; </span><br></pre></td></tr></table></div></figure>

        <h2 id="第三章"   >
          <a href="#第三章" class="heading-link"><i class="fas fa-link"></i></a><a href="#第三章" class="headerlink" title="第三章"></a>第三章</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"></span><br><span class="line">第三章：</span><br><span class="line">#<span class="number">3</span><span class="number">-5</span>：</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">int</span> a,b;</span><br><span class="line">	<span class="keyword">float</span> x;</span><br><span class="line">	<span class="keyword">double</span> y;</span><br><span class="line">	<span class="keyword">char</span> c1, c2;</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the Integer a,b:\n&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%d %d&quot;</span>,&amp;a, &amp;b);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the Float x:\n&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%f&quot;</span>,&amp;x);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the Double y:\n&quot;</span>);</span><br><span class="line">	<span class="built_in">scanf</span>(<span class="string">&quot;%lf&quot;</span>,&amp;y);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the char c1:\n&quot;</span>);</span><br><span class="line">	c1 = <span class="built_in">getchar</span>();</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;input the char c2:\n&quot;</span>);</span><br><span class="line">	c2 = <span class="built_in">getchar</span>();</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%d,%d,%.2f,%.2lf,%c,%c&quot;</span>,a,b,x,y,c1,c2);</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">#<span class="number">3.6</span>加密 将China译成密码 利用<span class="keyword">char</span>字符在计算机中存储ASCII码值 每个字符相加四个单位即可</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> c1,c2,c3,c4,c5;</span><br><span class="line">	c1 = <span class="string">&#x27;C&#x27;</span>;</span><br><span class="line">	c2 = <span class="string">&#x27;h&#x27;</span>;</span><br><span class="line">	c3 = <span class="string">&#x27;i&#x27;</span>;</span><br><span class="line">	c4 = <span class="string">&#x27;n&#x27;</span>;</span><br><span class="line">	c5 = <span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"></span><br><span class="line">	c1+=<span class="number">4</span>;</span><br><span class="line">	c2+=<span class="number">4</span>;</span><br><span class="line">	c3+=<span class="number">4</span>;</span><br><span class="line">	c4+=<span class="number">4</span>;</span><br><span class="line">	c5+=<span class="number">4</span>;</span><br><span class="line"></span><br><span class="line">	<span class="built_in">putchar</span>(c1);</span><br><span class="line">	<span class="built_in">putchar</span>(c2);</span><br><span class="line">	<span class="built_in">putchar</span>(c3);</span><br><span class="line">	<span class="built_in">putchar</span>(c4);</span><br><span class="line">	<span class="built_in">putchar</span>(c5);</span><br><span class="line"></span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;\n&quot;</span>);</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">printf</span>(<span class="string">&quot;%c%c%c%c%c&quot;</span>,c1,c2,c3,c4,c5);</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="第五章"   >
          <a href="#第五章" class="heading-link"><i class="fas fa-link"></i></a><a href="#第五章" class="headerlink" title="第五章"></a>第五章</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br></pre></td><td class="code"><pre><span class="line">第五章：</span><br><span class="line"><span class="number">5.3</span> 求最大公约数与最小公倍数</span><br><span class="line"></span><br><span class="line"><span class="number">5.3</span><span class="number">.1</span>:最大公约数：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求a b两个数字的最大公约数 （辗转相除法） </span></span><br><span class="line">	<span class="keyword">int</span> a, b, res = <span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	<span class="keyword">if</span>(a &lt; b) <span class="comment">//默认a&gt;b</span></span><br><span class="line">	&#123;</span><br><span class="line">		<span class="keyword">int</span> t = a;</span><br><span class="line">		a = b;</span><br><span class="line">		b = t;</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(a % b == <span class="number">0</span>)&#123;  <span class="comment">//如果a是b的倍数 那么最大公因数就是b </span></span><br><span class="line">		res = b; </span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">int</span> c = a % b;	<span class="comment">//否则先取余 余数在else语句中肯定是不为0的 </span></span><br><span class="line">		<span class="keyword">while</span>(c != <span class="number">0</span>)&#123;	<span class="comment">//若若满足不为0 则较小数作为a 余数作为b c重新被a b取余 </span></span><br><span class="line">			a = b; </span><br><span class="line">			b = c;</span><br><span class="line">			c = a % b;</span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		res = b;</span><br><span class="line">	&#125;</span><br><span class="line">	cout &lt;&lt; res;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.3</span><span class="number">.2</span>：最小公倍数：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求a b两个数字的最小公倍数</span></span><br><span class="line">	<span class="keyword">int</span> a,b,res=<span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; a &gt;&gt; b;</span><br><span class="line">	<span class="keyword">if</span>(a &lt; b)&#123; <span class="comment">//默认a大于b </span></span><br><span class="line">		<span class="keyword">int</span> t = a;</span><br><span class="line">		a = b;</span><br><span class="line">		b = t;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(a % b == <span class="number">0</span>)&#123; <span class="comment">//如果a是b的倍数 那么最小公倍数就是a </span></span><br><span class="line">		res = a; </span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">int</span> temp=b;</span><br><span class="line">		<span class="keyword">while</span>(temp&lt;=a*b)&#123; <span class="comment">//直到temp变成a*b </span></span><br><span class="line">			<span class="keyword">if</span>(temp%a==<span class="number">0</span>&amp;&amp;temp%b==<span class="number">0</span>)&#123; <span class="comment">//如果从较小数开始 一直自增 如果能同时取余两个数 那么就是公倍数 </span></span><br><span class="line">				res = temp;</span><br><span class="line">				<span class="keyword">break</span>;</span><br><span class="line">			&#125;</span><br><span class="line">			temp++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; res &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.8</span> 输出所有水仙花数(三位数，且各数的立方和等于该数本身)：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isNum</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">	<span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">v</span><span class="params">(<span class="number">1</span>, num)</span></span>;</span><br><span class="line">	vector&lt;<span class="keyword">int</span>&gt;::iterator it;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">while</span>(num)&#123;</span><br><span class="line">		v.<span class="built_in">push_back</span>(num%<span class="number">10</span>);</span><br><span class="line">		num /= <span class="number">10</span>;</span><br><span class="line">	&#125; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(it=v.<span class="built_in">begin</span>()+<span class="number">1</span>; it != v.<span class="built_in">end</span>(); it++)&#123;</span><br><span class="line">		sum += (<span class="built_in">pow</span>(*it, <span class="number">3</span>));</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(sum == v[<span class="number">0</span>])&#123;</span><br><span class="line">		flag = <span class="literal">true</span>;</span><br><span class="line">	&#125;	</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> flag;</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求所有的水仙花 </span></span><br><span class="line">	<span class="keyword">int</span> num;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">100</span>; i&lt;<span class="number">1000</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">isNum</span>(i))&#123;</span><br><span class="line">			cout &lt;&lt; i &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">		&#125;	</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.9</span>求<span class="number">1000</span>之内的所有完数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getNum</span><span class="params">(<span class="keyword">int</span> num)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> sum=<span class="number">0</span>; </span><br><span class="line">	<span class="function">vector&lt;<span class="keyword">int</span>&gt; <span class="title">v</span><span class="params">(<span class="number">1</span>, num)</span></span>;</span><br><span class="line">	vector&lt;<span class="keyword">int</span>&gt;::iterator it;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">1</span>; i&lt;num; i++)&#123; <span class="comment">//千万注意 取余的两边绝对！绝对不能为0 因此i不能是0 </span></span><br><span class="line">		<span class="keyword">if</span>(num%i==<span class="number">0</span>)</span><br><span class="line">			v.<span class="built_in">push_back</span>(i);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(it=v.<span class="built_in">begin</span>()+<span class="number">1</span>; it!=v.<span class="built_in">end</span>(); it++)&#123;</span><br><span class="line">		sum += (*it);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(sum == v[<span class="number">0</span>])&#123;</span><br><span class="line">		cout &lt;&lt; sum &lt;&lt; <span class="string">&quot;是完数&quot;</span> &lt;&lt; endl;</span><br><span class="line">	&#125;</span><br><span class="line">	v.<span class="built_in">clear</span>();</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="comment">//求完数 即一个数等于它所有的因子之和</span></span><br><span class="line">	<span class="keyword">int</span> num,end = <span class="number">1000</span>;</span><br><span class="line">	<span class="keyword">while</span>(num++ &lt; end)&#123;</span><br><span class="line">		<span class="built_in">getNum</span>(num);</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.10</span>：求 <span class="number">2</span>/<span class="number">1</span> + <span class="number">3</span>/<span class="number">2</span> + <span class="number">5</span>/<span class="number">3</span> +... 数列的前<span class="number">20</span>项和</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span> </span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span>&#123;</span><br><span class="line">	<span class="keyword">double</span> up=<span class="number">2</span>, down=<span class="number">1</span>,res = <span class="number">0</span>; <span class="comment">//up代表分母 down代表分子 </span></span><br><span class="line">	<span class="keyword">int</span> i=<span class="number">0</span>;</span><br><span class="line">	<span class="keyword">while</span>(i++&lt;<span class="number">20</span>)&#123;</span><br><span class="line">		res += (up/down);</span><br><span class="line">		up = up + down;</span><br><span class="line">		down = up - down; <span class="comment">//注意 这里不能用down = up  因为up 已经是 up+down </span></span><br><span class="line">						  <span class="comment">//而应该是新的up减去之前的down </span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; res;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">5.11</span>: 一个球从<span class="number">100</span>M高度落下 每次落地反弹回原高度的一般 再落下...求第<span class="number">10</span>次 共经过多少米 反弹多高？</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="built_in">main</span>()</span><br><span class="line">&#123;</span><br><span class="line"><span class="keyword">float</span> height=<span class="number">100.0</span>,sum_height=<span class="number">100</span>;</span><br><span class="line"><span class="keyword">int</span> i;</span><br><span class="line"><span class="keyword">for</span>(i=<span class="number">1</span>;i&lt;<span class="number">10</span>;i++) &#123;</span><br><span class="line">	height=height/<span class="number">2</span>;</span><br><span class="line">	sum_height+=height*<span class="number">2</span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;%fm\n&quot;</span>,sum_height);</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;%fm\n&quot;</span>,height);</span><br><span class="line"><span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="第六章："   >
          <a href="#第六章：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第六章：" class="headerlink" title="第六章："></a>第六章：</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br></pre></td><td class="code"><pre><span class="line">第六章：</span><br><span class="line">	例题：输入字符串 从中打印出单词的个数</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="built_in">main</span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">char</span> str[N];</span><br><span class="line">	<span class="keyword">int</span> num,word=<span class="number">0</span>;</span><br><span class="line">	<span class="built_in">gets</span>(str);</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; str[i]!=<span class="string">&#x27;\0&#x27;</span>; i++)&#123;</span><br><span class="line">		<span class="keyword">if</span>(str[i] == <span class="string">&#x27; &#x27;</span>)&#123; <span class="comment">//如果一开始就是空格 那么不是单词 </span></span><br><span class="line">			word = <span class="number">0</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(word==<span class="number">0</span>)&#123;	<span class="comment">//空格之后 认为是单词 记为遇到1个 </span></span><br><span class="line">			word = <span class="number">1</span>;</span><br><span class="line">			num++;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; num &lt;&lt;endl; </span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.5</span>：逆序存放升序的数组 比如处理前：<span class="number">1</span> <span class="number">2</span> <span class="number">3</span> <span class="number">4</span> <span class="number">5</span> 存放后为：<span class="number">5</span> <span class="number">4</span> <span class="number">3</span> <span class="number">2</span> <span class="number">1</span> </span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="built_in">main</span>()</span><br><span class="line">&#123;</span><br><span class="line">	<span class="keyword">int</span> arr[N],len;</span><br><span class="line">	stack&lt;<span class="keyword">int</span>&gt; s;</span><br><span class="line">	</span><br><span class="line">	cin &gt;&gt; len;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)</span><br><span class="line">	&#123;</span><br><span class="line">		cin &gt;&gt; arr[i];</span><br><span class="line">		s.<span class="built_in">push</span>(arr[i]);  <span class="comment">//利用栈的FILO原理 从头到尾将元素进行入栈</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;处理前的数组元素分别为：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;len; i++)</span><br><span class="line">		cout &lt;&lt; arr[i] &lt;&lt; <span class="string">&quot; &quot;</span>; 		</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">		arr[i] = s.<span class="built_in">top</span>();</span><br><span class="line">		s.<span class="built_in">pop</span>();</span><br><span class="line">	&#125;	</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;处理后的数组元素分别为：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;len; i++)</span><br><span class="line">		cout &lt;&lt; arr[i] &lt;&lt; <span class="string">&quot; &quot;</span>; 		</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>

<figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">6.9</span>：</span><br><span class="line">有若干个数字在有序数组中 此时要求实现折半查找方式来查找某个元素</span><br><span class="line">并给出它的位置 若找不到该数 则输出 无此数</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> a[<span class="number">6</span>] = &#123;<span class="number">1</span>, <span class="number">4</span>, <span class="number">6</span>, <span class="number">32</span>, <span class="number">58</span>, <span class="number">66</span>&#125;;</span><br><span class="line">	<span class="keyword">int</span> left = <span class="number">0</span>;</span><br><span class="line">	<span class="keyword">int</span> right = <span class="number">5</span>; <span class="comment">//注意是最后一个元素 即 长度-1</span></span><br><span class="line">	<span class="keyword">int</span> mid,num;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入欲查找的数字：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; num;</span><br><span class="line">	<span class="keyword">while</span>(left &lt;= right)&#123; <span class="comment">//注意条件</span></span><br><span class="line">		mid = (left + right) / <span class="number">2</span>;</span><br><span class="line">		<span class="keyword">if</span>(a[mid] == num)&#123;</span><br><span class="line">			cout &lt;&lt; <span class="string">&quot;已找到，是第&quot;</span> &lt;&lt; mid+<span class="number">1</span> &lt;&lt; <span class="string">&quot;个元素&quot;</span> &lt;&lt;endl;</span><br><span class="line">			<span class="keyword">break</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(a[mid] &lt; num)&#123; </span><br><span class="line">			left = mid + <span class="number">1</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			right = mid - <span class="number">1</span>;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(left &gt; right)&#123;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;无此数&quot;</span> &lt;&lt;endl;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">统计各字符</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> str[<span class="number">2</span>][<span class="number">10</span>];</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;<span class="number">2</span>; i++)</span><br><span class="line">		<span class="built_in">gets</span>(str[i]);      <span class="comment">//for循环每次输入该行</span></span><br><span class="line"></span><br><span class="line">	cout &lt;&lt; endl;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//统计大写字母 小写字母 数字 空格以及其他字符 </span></span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> num=<span class="number">0</span>, U_char=<span class="number">0</span>, L_char=<span class="number">0</span>, space=<span class="number">0</span>, oth=<span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i =<span class="number">0</span>; i&lt;<span class="number">2</span>; i++) &#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;<span class="number">10</span>; j++)&#123;</span><br><span class="line">			<span class="keyword">char</span> ch= str[i][j];</span><br><span class="line">			<span class="keyword">if</span>(ch &gt;= <span class="string">&#x27;0&#x27;</span> &amp;&amp; ch &lt;= <span class="string">&#x27;9&#x27;</span> )&#123;</span><br><span class="line">				num++;				</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(ch &gt;= <span class="string">&#x27;A&#x27;</span> &amp;&amp; ch &lt;= <span class="string">&#x27;Z&#x27;</span>)&#123;</span><br><span class="line">				U_char++;</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(ch &gt;= <span class="string">&#x27;a&#x27;</span> &amp;&amp; ch &lt;= <span class="string">&#x27;z&#x27;</span>)&#123;</span><br><span class="line">				L_char++;</span><br><span class="line">			&#125;<span class="keyword">else</span> <span class="keyword">if</span>(ch == <span class="string">&#x27; &#x27;</span>)&#123;</span><br><span class="line">				space++;</span><br><span class="line">			&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">				oth++;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;数字：&quot;</span> &lt;&lt; num&lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;大写字母：&quot;</span> &lt;&lt; U_char &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;小写字母:&quot;</span> &lt;&lt; L_char &lt;&lt; endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;空格：&quot;</span> &lt;&lt;space &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;其他：&quot;</span> &lt;&lt; oth &lt;&lt;endl;</span><br><span class="line"></span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.14</span></span><br><span class="line">比较两个字符串 其中返回正数 该正数表示相差的总值</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//两个字符串的比较 返回总相差的ASCII码值</span></span><br><span class="line">	<span class="keyword">char</span> c1[N], c2[N];</span><br><span class="line">	<span class="keyword">int</span> len1, len2;</span><br><span class="line">	<span class="keyword">int</span> sum=<span class="number">0</span>;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串一：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="built_in">gets</span>(c1);</span><br><span class="line">	len1 = <span class="built_in">strlen</span>(c1);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串二：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="built_in">gets</span>(c2);</span><br><span class="line">	len2 = <span class="built_in">strlen</span>(c2);</span><br><span class="line">	<span class="keyword">int</span> len = (len1&gt;len2)? len1: len2;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;len; i++)&#123;</span><br><span class="line">	   sum += <span class="built_in">abs</span>(c1[i]-c2[i]);</span><br><span class="line">	&#125;</span><br><span class="line">	 </span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;两者相差：&quot;</span> &lt;&lt; sum &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.15</span> 将字符数组a2的所有字符都复制到a1中去 其中包括a2的结束字符<span class="string">&#x27;\0&#x27;</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"> </span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="comment">//两个字符串的比较 返回总相差的ASCII码值</span></span><br><span class="line">	<span class="keyword">char</span> c1[N], c2[N]=&#123;<span class="string">&#x27;v&#x27;</span>,<span class="string">&#x27;i&#x27;</span>,<span class="string">&#x27;n&#x27;</span>,<span class="string">&#x27;c&#x27;</span>,<span class="string">&#x27;\0&#x27;</span>&#125;;</span><br><span class="line">	<span class="keyword">int</span> len1, len2;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串一：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="built_in">gets</span>(c1);</span><br><span class="line">	len1 = <span class="built_in">strlen</span>(c1);</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;请输入字符串二：&quot;</span> &lt;&lt;endl;</span><br><span class="line"></span><br><span class="line">	len2 = <span class="built_in">strlen</span>(c2)+<span class="number">1</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> k = len1,i=<span class="number">0</span>; k&lt;len1+len2; k++,i++)&#123;</span><br><span class="line">		c1[k] = c2[i];</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> k = <span class="number">0</span>; k&lt;len1+len2; k++)&#123;</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c它的ASCII值为%d\n&quot;</span>, c1[k],c1[k]);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></div></figure>


        <h2 id="第七章："   >
          <a href="#第七章：" class="heading-link"><i class="fas fa-link"></i></a><a href="#第七章：" class="headerlink" title="第七章："></a>第七章：</h2>
      <figure class="highlight cpp"><div class="table-container"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="number">7.10</span>：写一个函数，输入一行字符，将最长的单词输出：</span><br><span class="line">【转】：https:<span class="comment">//blog.csdn.net/navicheung/article/details/77990479</span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//最长单词函数</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">getLongWord</span><span class="params">(<span class="keyword">char</span> str[])</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">int</span> max_length=<span class="number">0</span>, len=<span class="number">0</span>;</span><br><span class="line">    <span class="keyword">char</span> temp[N], res[N];</span><br><span class="line">    <span class="keyword">int</span> n = <span class="built_in">strlen</span>(str);</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;=n; i++)&#123;</span><br><span class="line">    	</span><br><span class="line">		<span class="comment">//如果字符是单词的一部分 加入到temp数组 </span></span><br><span class="line">        <span class="keyword">if</span> ((str[i]&gt;=<span class="string">&#x27;a&#x27;</span>&amp;&amp;str[i]&lt;=<span class="string">&#x27;z&#x27;</span>)||(str[i]&gt;=<span class="string">&#x27;A&#x27;</span>&amp;&amp;str[i]&lt;=<span class="string">&#x27;Z&#x27;</span>)) </span><br><span class="line">            temp[len++]=str[i];</span><br><span class="line">        <span class="keyword">else</span>&#123;</span><br><span class="line">            <span class="comment">//若不是单词 那么temp数组不再增添元素 而是结束 </span></span><br><span class="line">			temp[len]=<span class="string">&#x27;\0&#x27;</span>;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">//如果每次长度有更长的 更新 且记得len重新赋值为0 以下一次判断 </span></span><br><span class="line">			<span class="comment">//最终的结果存入ma数组中 关键语句 </span></span><br><span class="line">            len&gt;max_length ? max_length=len, <span class="built_in">strcpy</span>(res, temp), len=<span class="number">0</span> : len=<span class="number">0</span>; </span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;最长的单词是： &quot;</span>);</span><br><span class="line">    <span class="built_in">puts</span>(res);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> str[N];</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;请输入字符串：\n &quot;</span>);       <span class="comment">//输入一行字符</span></span><br><span class="line">    <span class="built_in">gets</span>(str);</span><br><span class="line">    </span><br><span class="line">    <span class="built_in">getLongWord</span>(str);                         <span class="comment">//调用最长单词函数</span></span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.11</span> 利用起泡法对输入的n个字符进行升序排序：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">swap</span><span class="params">(<span class="keyword">char</span> &amp;a, <span class="keyword">char</span> &amp;b)</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> t = a;</span><br><span class="line">	a = b;</span><br><span class="line">	b = t;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> str[N];</span><br><span class="line">   	<span class="keyword">int</span> length;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;你要输入几个字符：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; length;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;依次输入这些字符：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)&#123;</span><br><span class="line">		cin &gt;&gt; str[i];</span><br><span class="line">	&#125;</span><br><span class="line">	 </span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)&#123;</span><br><span class="line">		<span class="keyword">for</span>(<span class="keyword">int</span> j=<span class="number">0</span>; j&lt;length-i<span class="number">-1</span>; j++)&#123;</span><br><span class="line">			<span class="keyword">if</span>(str[j] &gt; str[j+<span class="number">1</span>])&#123;</span><br><span class="line">				<span class="built_in">swap</span>(str[j], str[j+<span class="number">1</span>]);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;起泡法排序结果：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=<span class="number">0</span>; i&lt;length; i++)</span><br><span class="line">		cout &lt;&lt; str[i] &lt;&lt; <span class="string">&quot; &quot;</span>;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.16</span> 输入一个十六进制数字（字符串的形式），输出相应的十进制数字：</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//十六进制数转换为十进制数</span></span><br><span class="line"><span class="comment">//相应的位置 按权展开即可</span></span><br><span class="line"><span class="comment">//A2 = A*10^1 + 2*10^0 </span></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">fun</span><span class="params">(<span class="keyword">char</span> str[])</span></span>&#123;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">int</span> n = <span class="built_in">strlen</span>(str); <span class="comment">//n代表字符串的长度 即代表十六进制数是n位数 </span></span><br><span class="line">	<span class="keyword">int</span> sum =<span class="number">0</span>;</span><br><span class="line">	</span><br><span class="line">	<span class="keyword">for</span>(<span class="keyword">int</span> i=n<span class="number">-1</span>,j=<span class="number">0</span>; i&gt;=<span class="number">0</span>; i--)&#123;</span><br><span class="line">		<span class="keyword">int</span> temp = <span class="number">0</span>;</span><br><span class="line">		<span class="keyword">if</span>(str[i]==<span class="string">&#x27;A&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">10</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;B&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">11</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;C&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">12</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;D&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">13</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;E&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">14</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span> <span class="keyword">if</span>(str[i]==<span class="string">&#x27;F&#x27;</span>)&#123;</span><br><span class="line">			temp = <span class="number">15</span>;</span><br><span class="line">		&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">			temp = str[i] - <span class="string">&#x27;0&#x27;</span>;</span><br><span class="line">		&#125;</span><br><span class="line">		sum += (temp * <span class="built_in">pow</span>(<span class="number">16</span>, j++));</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> sum;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    <span class="keyword">char</span> str[N];</span><br><span class="line">    <span class="built_in">gets</span>(str);</span><br><span class="line">    </span><br><span class="line">    cout &lt;&lt; <span class="string">&quot;十进制数是：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cout &lt;&lt; <span class="built_in">fun</span>(str) &lt;&lt;endl;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">7.17</span> 递归法 将一个整数转换为字符串</span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="comment">//将一个整数 转换成字符串 </span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> n)</span></span>&#123;</span><br><span class="line">	<span class="keyword">char</span> ch; <span class="comment">//每次输出的字符 </span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//1.递归的第一步 首先想好边界情况 这里每次i位数每次取i-1 直到结果为0 </span></span><br><span class="line">	<span class="keyword">if</span>(n==<span class="number">0</span>)&#123;  </span><br><span class="line">		<span class="keyword">return</span>;	</span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;		</span><br><span class="line">	<span class="comment">//2.否则 执行每一次的操作具体的行为</span></span><br><span class="line">	<span class="comment">//注意这里fun一直到边界才输出！因此结果是12（假如n=12）</span></span><br><span class="line">	<span class="comment">//如果fun放在末尾 那么会先输出数字 因而结果会是21 </span></span><br><span class="line">		<span class="built_in">fun</span>(n/<span class="number">10</span>);</span><br><span class="line">		<span class="built_in">printf</span>(<span class="string">&quot;%c&quot;</span>, (n%<span class="number">10</span>) + <span class="string">&#x27;0&#x27;</span>);   				</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> num =<span class="number">0</span>;</span><br><span class="line">	cin &gt;&gt; num;</span><br><span class="line">	</span><br><span class="line">	<span class="built_in">fun</span>(num);</span><br><span class="line">	</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="number">6.18</span>：给出年、月、日 计算是该年的第几天</span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;iostream&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cstring&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;cmath&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;vector&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stack&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> N 100 </span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> std;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">bool</span> <span class="title">isLeapYear</span><span class="params">(<span class="keyword">int</span> year)</span></span>&#123;</span><br><span class="line">	<span class="keyword">bool</span> flag = <span class="literal">false</span>;</span><br><span class="line">	<span class="keyword">if</span>( year%<span class="number">400</span>==<span class="number">0</span> || (year%<span class="number">4</span>==<span class="number">0</span>&amp;&amp;year%<span class="number">100</span>!=<span class="number">0</span>) )&#123;  </span><br><span class="line">	<span class="comment">//1.被400整除的年份是闰年</span></span><br><span class="line">	<span class="comment">//2.被4整除同时不能被100所整除的年份是闰年 </span></span><br><span class="line">		flag = <span class="literal">true</span>;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">return</span> flag;	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//输入某年某月某日 判断这是该年中的第几天</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">fun</span><span class="params">(<span class="keyword">int</span> year, <span class="keyword">int</span> month, <span class="keyword">int</span> day)</span></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> months[<span class="number">12</span>] = &#123;<span class="number">31</span>, <span class="number">28</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>, <span class="number">30</span>, <span class="number">31</span>&#125;; <span class="comment">//非闰年的各月份天数数组 </span></span><br><span class="line">	<span class="keyword">int</span> days_sum = <span class="number">0</span>,i=<span class="number">0</span>; </span><br><span class="line">	</span><br><span class="line">	<span class="keyword">if</span>(month&lt;<span class="number">2</span>)&#123; <span class="comment">//如果没到2月 则不用计算是不是闰年 </span></span><br><span class="line">		days_sum = day;</span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;该日期是第&quot;</span> &lt;&lt; days_sum &lt;&lt; <span class="string">&quot;天&quot;</span> &lt;&lt;endl; </span><br><span class="line">	&#125;<span class="keyword">else</span>&#123;</span><br><span class="line">		<span class="keyword">if</span>(<span class="built_in">isLeapYear</span>(year))&#123;</span><br><span class="line">			months[<span class="number">1</span>] = <span class="number">29</span>; <span class="comment">//闰年2月有29天 </span></span><br><span class="line">		&#125;</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">while</span>(i&lt;month<span class="number">-1</span>)&#123; <span class="comment">//计算该月之前有多少天</span></span><br><span class="line">			days_sum += months[i];</span><br><span class="line">			i++;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="comment">//然后该月第几天加上去</span></span><br><span class="line">		cout &lt;&lt; <span class="string">&quot;该日期是第&quot;</span> &lt;&lt; days_sum+day &lt;&lt; <span class="string">&quot;天&quot;</span> &lt;&lt;endl; </span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">int</span> year, month, day;</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入年份：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; year;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入月：&quot;</span> &lt;&lt;endl;</span><br><span class="line">	cin &gt;&gt; month;</span><br><span class="line">	</span><br><span class="line">	cout &lt;&lt; <span class="string">&quot;输入日：&quot;</span> &lt;&lt; endl;</span><br><span class="line">	cin &gt;&gt; day;</span><br><span class="line">	</span><br><span class="line">	<span class="comment">//可以添加判断日期是否合法的函数... 现假设合法 正确输入格式 </span></span><br><span class="line">	</span><br><span class="line">	<span class="built_in">fun</span>(year, month, day);</span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></div></figure>
</div><footer class="post-footer"><div class="post-ending ending"><div class="ending__text">------ 本文结束，感谢您的阅读 ------</div></div><div class="post-copyright copyright"><div class="copyright-author"><span class="copyright-author__name">本文作者: </span><span class="copyright-author__value"><a href="https://bruce-jj.gitee.io/blog">何水浪</a></span></div><div class="copyright-link"><span class="copyright-link__name">本文链接: </span><span class="copyright-link__value"><a href="https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%20%E7%BC%96%E7%A8%8B%E5%B8%B8%E8%A7%81%E4%BB%A3%E7%A0%81/">https://bruce-jj.gitee.io/blog/2021/03/11/C_C++%20%E7%BC%96%E7%A8%8B%E5%B8%B8%E8%A7%81%E4%BB%A3%E7%A0%81/</a></span></div><div class="copyright-notice"><span class="copyright-notice__name">版权声明: </span><span class="copyright-notice__value">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" rel="external nofollow" target="_blank">BY-NC-SA</a> 许可协议。转载请注明出处！</span></div></div><div class="post-tags"><span class="post-tags-item"><span class="post-tags-item__icon"><i class="fas fa-tag"></i></span><a class="post-tags-item__link" href="https://bruce-jj.gitee.io/blog/tags/C%E8%AF%AD%E8%A8%80/">C语言</a></span></div><nav class="post-paginator paginator"><div class="paginator-prev"><a class="paginator-prev__link" href="/blog/2021/03/11/%E4%B8%8D%E4%BC%9A%E5%90%A7%E8%80%83%E7%A0%94%E8%8B%B1%E8%AF%AD/"><span class="paginator-prev__icon"><i class="fas fa-angle-left"></i></span><span class="paginator-prev__text">不会吧？都2021年了，还有人觉得考研英语简单！？</span></a></div><div class="paginator-next"><a class="paginator-next__link" href="/blog/2021/03/11/C_C++%E5%8A%9B%E6%89%A3LeetCode%E8%BD%AC%E8%BD%BD/"><span class="paginator-prev__text">C/C++力扣LeetCode转载</span><span class="paginator-next__icon"><i class="fas fa-angle-right"></i></span></a></div></nav></footer></div></div><div class="comments" id="comments"><div id="valine-container"></div></div></div><div class="sidebar-wrap" id="sidebar-wrap"><aside class="sidebar" id="sidebar"><div class="sidebar-nav"><span class="sidebar-nav-toc current">文章目录</span><span class="sidebar-nav-ov">站点概览</span></div><section class="sidebar-toc"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-%E5%8A%A0%E6%B3%95%E5%99%A8%E3%80%81%E7%B4%AF%E5%8A%A0%E5%99%A8%EF%BC%9A%EF%BC%88%E5%A4%9A%E5%AE%9E%E7%8E%B0-1-2-%E2%80%A6-n-%E4%BB%A5%E5%8F%8A%E9%98%B6%E4%B9%98-n-%E7%9A%84%E5%8A%9F%E8%83%BD%EF%BC%89"><span class="toc-number">1.</span> <span class="toc-text">
          1.加法器、累加器：（多实现 1+2+…+n 以及阶乘 n! 的功能）</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E6%B1%82%E7%B4%AF%E5%8A%A0"><span class="toc-number">1.0.1.</span> <span class="toc-text">
          一、求累加</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E6%B1%82%E6%9F%90%E6%95%B0%E9%98%B6%E4%B9%98"><span class="toc-number">1.0.2.</span> <span class="toc-text">
          二、求某数阶乘</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-%E6%B1%82%E4%B8%A4%E4%B8%AA%E6%95%B0%E6%88%96%E6%95%B0%E7%BB%84%E4%B8%AD%E7%9A%84%E6%9C%80%E5%A4%A7%E5%80%BC%E4%B8%8E%E6%9C%80%E5%B0%8F%E5%80%BC"><span class="toc-number">2.</span> <span class="toc-text">
          2. 求两个数或数组中的最大值与最小值</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F%EF%BC%8C%E5%BF%85%E9%A1%BB%E4%BC%9A%E9%BB%98%E5%86%99"><span class="toc-number">3.</span> <span class="toc-text">
          3.冒泡排序，必须会默写</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-%E7%AE%80%E5%8D%95%E7%9A%84switch-case%E5%A4%9A%E5%88%86%E6%94%AF%E6%9D%A1%E4%BB%B6%E8%AF%AD%E5%8F%A5"><span class="toc-number">4.</span> <span class="toc-text">
          4.简单的switch-case多分支条件语句</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-%E6%B1%82%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%9A%84%E6%9C%80%E5%A4%A7%E5%85%AC%E7%BA%A6%E6%95%B0%E3%80%81%E6%9C%80%E5%B0%8F%E5%85%AC%E5%80%8D%E6%95%B0-%E4%B8%8D%E5%8F%AF%E5%90%AB0-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">5.</span> <span class="toc-text">
          5.求两个数的最大公约数、最小公倍数(不可含0) (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97%E9%80%92%E5%BD%92"><span class="toc-number">6.</span> <span class="toc-text">
          6.斐波那契数列递归</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#7-%E5%88%A4%E6%96%AD%E6%9F%90%E6%95%B0%E6%98%AF%E5%90%A6%E6%98%AF%E7%B4%A0%E6%95%B0-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">7.</span> <span class="toc-text">
          7.判断某数是否是素数 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#8-%E5%A4%A7%E5%86%99%E5%AD%97%E6%AF%8D%E8%BD%AC%E5%8C%96%E4%B8%BA%E5%B0%8F%E5%86%99%E5%AD%97%E6%AF%8D"><span class="toc-number">8.</span> <span class="toc-text">
          8.大写字母转化为小写字母</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#9-%E7%BB%9F%E8%AE%A1%E8%BE%93%E5%85%A5%E5%AD%97%E7%AC%A6%E4%B8%AD%E5%8D%95%E8%AF%8D%E7%9A%84%E4%B8%AA%E6%95%B0-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">9.</span> <span class="toc-text">
          9.统计输入字符中单词的个数 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#10-%E6%B1%82Sn-a-aa-aaa-%E2%80%A6-aa%E2%80%A6aaa%EF%BC%88%E6%9C%89n%E4%B8%AAa%EF%BC%89%E4%B9%8B%E5%80%BC%EF%BC%8C%E5%85%B6%E4%B8%ADa%E6%98%AF%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%EF%BC%8C%E4%B8%BA2%E3%80%82-%E4%BE%8B%E5%A6%82%EF%BC%8Cn-5%E6%97%B6-2-22-222-2222-22222%EF%BC%8Cn%E7%94%B1%E9%94%AE%E7%9B%98%E8%BE%93%E5%85%A5%E3%80%82-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">10.</span> <span class="toc-text">
          10.求Sn&#x3D;a+aa+aaa+…+aa…aaa（有n个a）之值，其中a是一个数字，为2。 例如，n&#x3D;5时&#x3D;2+22+222+2222+22222，n由键盘输入。 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#11-%E6%B1%82Sn-1-2-3-4-5-%E2%80%A6-n-%E4%B9%8B%E5%80%BC%EF%BC%8C%E5%85%B6%E4%B8%ADn%E6%98%AF%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97-n%E4%B8%8D%E8%B6%85%E8%BF%8720-%E6%B3%A8%E6%84%8F%E4%BD%BF%E7%94%A8int%E7%B1%BB%E5%9E%8B-%E5%BD%93n%E4%B8%BA19%E6%97%B6%E4%BC%9A%E6%BA%A2%E5%87%BA-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">11.</span> <span class="toc-text">
          11.求Sn&#x3D;1!+2!+3!+4!+5!+…+n!之值，其中n是一个数字(n不超过20) 注意使用int类型 当n为19时会溢出 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#12-%E6%B1%82%E4%B8%80%E4%B8%AA3%C3%973%E7%9F%A9%E9%98%B5%E5%AF%B9%E8%A7%92%E7%BA%BF%E5%85%83%E7%B4%A0%E4%B9%8B%E5%92%8C%E3%80%82-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">12.</span> <span class="toc-text">
          12.求一个3×3矩阵对角线元素之和。 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#13-%E7%94%A8%E9%80%89%E6%8B%A9%E6%B3%95%E5%AF%B910%E4%B8%AA%E6%95%B4%E6%95%B0%E4%BB%8E%E5%B0%8F%E5%88%B0%E5%A4%A7%E6%8E%92%E5%BA%8F%E3%80%82-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">13.</span> <span class="toc-text">
          13.用选择法对10个整数从小到大排序。 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#14-%E6%95%B0%E7%BB%84%E6%8F%92%E5%85%A5%E5%A4%84%E7%90%86%EF%BC%8C%E5%AF%B9%E4%B8%80%E4%B8%AA%E6%8E%92%E5%A5%BD%E5%BA%8F%E7%9A%84%E6%95%B0%E7%BB%84%EF%BC%8C%E6%8C%89%E5%8E%9F%E6%9D%A5%E7%9A%84%E8%A7%84%E5%BE%8B%E6%8F%92%E5%85%A5%E6%9F%90%E6%95%B0"><span class="toc-number">14.</span> <span class="toc-text">
          14.数组插入处理，对一个排好序的数组，按原来的规律插入某数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#15-%E5%86%99%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0%EF%BC%8C%E4%BD%BF%E7%BB%99%E5%AE%9A%E7%9A%84%E4%B8%80%E4%B8%AA%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%EF%BC%88%EF%BC%93%C3%97%EF%BC%93%EF%BC%89%E8%BD%AC%E7%BD%AE%EF%BC%8C%E5%8D%B3%E8%A1%8C%E5%88%97%E4%BA%92%E6%8D%A2%E3%80%82-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">15.</span> <span class="toc-text">
          15.写一个函数，使给定的一个二维数组（３×３）转置，即行列互换。 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#16-%E8%87%AA%E5%AE%9A%E4%B9%89%E5%87%BD%E6%95%B0%E4%B9%8B%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8F%8D%E8%BD%AC"><span class="toc-number">16.</span> <span class="toc-text">
          16.自定义函数之字符串反转</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#17-%E5%86%99%E4%B8%80%E5%87%BD%E6%95%B0%EF%BC%8C%E5%B0%86%E4%B8%A4%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BF%9E%E6%8E%A5"><span class="toc-number">17.</span> <span class="toc-text">
          17.写一函数，将两个字符串连接</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#18-%E5%86%99%E4%B8%80%E5%87%BD%E6%95%B0%EF%BC%8C%E8%BE%93%E5%85%A5%E4%B8%80%E4%B8%AA%E5%9B%9B%E4%BD%8D%E6%95%B0%E5%AD%97%EF%BC%8C%E8%A6%81%E6%B1%82%E8%BE%93%E5%87%BA%E8%BF%99%E5%9B%9B%E4%B8%AA%E6%95%B0%E5%AD%97%E5%AD%97%E7%AC%A6%EF%BC%8C%E4%BD%86%E6%AF%8F%E4%B8%A4%E4%B8%AA%E6%95%B0%E5%AD%97%E9%97%B4%E7%A9%BA%E6%A0%BC%E3%80%82%E5%A6%82%E8%BE%93%E5%85%A51990%EF%BC%8C%E5%BA%94%E8%BE%93%E5%87%BA%E2%80%9D1-9-9-0%E2%80%9D%E3%80%82-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">18.</span> <span class="toc-text">
          18.写一函数，输入一个四位数字，要求输出这四个数字字符，但每两个数字间空格。如输入1990，应输出”1 9 9 0”。 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#19-%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E5%B8%A6%E5%8F%82%E7%9A%84%E5%AE%8F%EF%BC%8C%E4%BD%BF%E4%B8%A4%E4%B8%AA%E5%8F%82%E6%95%B0%E7%9A%84%E5%80%BC%E4%BA%92%E6%8D%A2%EF%BC%8C%E5%B9%B6%E5%86%99%E5%87%BA%E7%A8%8B%E5%BA%8F%EF%BC%8C%E8%BE%93%E5%85%A5%E4%B8%A4%E4%B8%AA%E6%95%B0%E4%BD%9C%E4%B8%BA%E4%BD%BF%E7%94%A8%E5%AE%8F%E6%97%B6%E7%9A%84%E5%AE%9E%E5%8F%82%E3%80%82%E8%BE%93%E5%87%BA%E5%B7%B2%E4%BA%A4%E6%8D%A2%E5%90%8E%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%80%BC%E3%80%82"><span class="toc-number">19.</span> <span class="toc-text">
          19.定义一个带参的宏，使两个参数的值互换，并写出程序，输入两个数作为使用宏时的实参。输出已交换后的两个值。</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#20-%E8%BE%93%E5%85%A5%E4%B8%A4%E4%B8%AA%E6%95%B4%E6%95%B0%EF%BC%8C%E6%B1%82%E4%BB%96%E4%BB%AC%E7%9B%B8%E9%99%A4%E7%9A%84%E4%BD%99%E6%95%B0%E3%80%82%E7%94%A8%E5%B8%A6%E5%8F%82%E7%9A%84%E5%AE%8F%E6%9D%A5%E5%AE%9E%E7%8E%B0%EF%BC%8C%E7%BC%96%E7%A8%8B%E5%BA%8F%E3%80%82"><span class="toc-number">20.</span> <span class="toc-text">
          20.输入两个整数，求他们相除的余数。用带参的宏来实现，编程序。</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#21-%E8%BE%93%E5%85%A5%E4%B8%80%E8%A1%8C%E7%94%B5%E6%8A%A5%E6%96%87%E5%AD%97%EF%BC%8C%E5%B0%86%E5%AD%97%E6%AF%8D%E5%8F%98%E6%88%90%E5%85%B6%E4%B8%8B%E4%B8%80%E5%AD%97%E6%AF%8D%EF%BC%88%E5%A6%82%E2%80%99a%E2%80%99%E5%8F%98%E6%88%90%E2%80%99b%E2%80%99%E2%80%A6%E2%80%A6%E2%80%99z%E2%80%99%E5%8F%98%E6%88%90%E2%80%99a%E2%80%99%E5%85%B6%E5%AE%83%E5%AD%97%E7%AC%A6%E4%B8%8D%E5%8F%98%EF%BC%89%E3%80%82"><span class="toc-number">21.</span> <span class="toc-text">
          21.输入一行电报文字，将字母变成其下一字母（如’a’变成’b’……’z’变成’ａ’其它字符不变）。</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#22-%E7%8E%B0%E6%9C%89N%E4%B8%AA%E5%AD%A6%E7%94%9F%E7%9A%84%E6%95%B0%E6%8D%AE%E8%AE%B0%E5%BD%95%EF%BC%8C%E6%AF%8F%E4%B8%AA%E8%AE%B0%E5%BD%95%E5%8C%85%E6%8B%AC%E5%AD%A6%E5%8F%B7%E3%80%81%E5%A7%93%E5%90%8D%E3%80%81%E4%B8%89%E7%A7%91%E6%88%90%E7%BB%A9%E3%80%82-%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0input-%E7%94%A8%E6%9D%A5%E8%BE%93%E5%85%A5%E4%B8%80%E4%B8%AA%E5%AD%A6%E7%94%9F%E7%9A%84%E6%95%B0%E6%8D%AE%E8%AE%B0%E5%BD%95%E3%80%82-%E7%BC%96%E5%86%99%E4%B8%80%E4%B8%AA%E5%87%BD%E6%95%B0print-%E6%89%93%E5%8D%B0%E4%B8%80%E4%B8%AA%E5%AD%A6%E7%94%9F%E7%9A%84%E6%95%B0%E6%8D%AE%E8%AE%B0%E5%BD%95%E3%80%82-%E5%9C%A8%E4%B8%BB%E5%87%BD%E6%95%B0%E8%B0%83%E7%94%A8%E8%BF%99%E4%B8%A4%E4%B8%AA%E5%87%BD%E6%95%B0%EF%BC%8C%E8%AF%BB%E5%8F%96N%E6%9D%A1%E8%AE%B0%E5%BD%95%E8%BE%93%E5%85%A5%EF%BC%8C%E5%86%8D%E6%8C%89%E8%A6%81%E6%B1%82%E8%BE%93%E5%87%BA%E3%80%82-N-lt-100"><span class="toc-number">22.</span> <span class="toc-text">
          22.现有N个学生的数据记录，每个记录包括学号、姓名、三科成绩。 编写一个函数input,用来输入一个学生的数据记录。 编写一个函数print,打印一个学生的数据记录。 在主函数调用这两个函数，读取N条记录输入，再按要求输出。 N&lt;100</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#23-%E5%B0%86%E4%B8%80%E4%B8%AA%E5%AD%97%E7%AC%A6%E4%B8%B2str%E7%9A%84%E5%86%85%E5%AE%B9%E9%A2%A0%E5%80%92%E8%BF%87%E6%9D%A5%EF%BC%8C%E5%B9%B6%E8%BE%93%E5%87%BA%E3%80%82str%E7%9A%84%E9%95%BF%E5%BA%A6%E4%B8%8D%E8%B6%85%E8%BF%87100%E4%B8%AA%E5%AD%97%E7%AC%A6%E3%80%82"><span class="toc-number">23.</span> <span class="toc-text">
          23.将一个字符串str的内容颠倒过来，并输出。str的长度不超过100个字符。</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#24-%E8%A7%A3%E4%B8%80%E5%85%83%E4%BA%8C%E6%AC%A1%E6%96%B9%E7%A8%8B-%E8%B0%AD%E6%B5%A9%E5%BC%BAC%E8%AF%AD%E8%A8%80%E4%BE%8B%E9%A2%98%EF%BC%89"><span class="toc-number">24.</span> <span class="toc-text">
          24.解一元二次方程 (谭浩强C语言例题）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#25-%E8%BE%93%E5%85%A5%E5%AD%97%E7%AC%A6%E4%B8%B2-%E5%B0%86%E7%BB%84%E6%88%90%E5%AD%97%E7%AC%A6%E7%9A%84%E6%89%80%E6%9C%89%E9%9D%9E%E8%8B%B1%E6%96%87%E5%AD%97%E6%AF%8D%E7%9A%84%E5%AD%97%E7%AC%A6%E5%88%A0%E9%99%A4%E5%90%8E%E8%BE%93%E5%87%BA"><span class="toc-number">25.</span> <span class="toc-text">
          25.输入字符串 将组成字符的所有非英文字母的字符删除后输出</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B0%AD%E6%B5%A9%E5%BC%BA%E7%AC%AC%E5%9B%9B%E7%89%88%E8%AF%BE%E5%90%8E%E4%B9%A0%E9%A2%98%E7%AD%94%E6%A1%88%EF%BC%9A"><span class="toc-number">26.</span> <span class="toc-text">
          谭浩强第四版课后习题答案：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%B8%89%E7%AB%A0"><span class="toc-number">27.</span> <span class="toc-text">
          第三章</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%BA%94%E7%AB%A0"><span class="toc-number">28.</span> <span class="toc-text">
          第五章</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E5%85%AD%E7%AB%A0%EF%BC%9A"><span class="toc-number">29.</span> <span class="toc-text">
          第六章：</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AC%AC%E4%B8%83%E7%AB%A0%EF%BC%9A"><span class="toc-number">30.</span> <span class="toc-text">
          第七章：</span></a></li></ol></section><!-- ov = overview--><section class="sidebar-ov hide"><div class="sidebar-ov-author"><div class="sidebar-ov-author__avatar"><img class="sidebar-ov-author__avatar_img" src="/blog/images/avatar.jpg" alt="avatar"></div><p class="sidebar-ov-author__text">C'est la vie</p></div><div class="sidebar-ov-state"><a class="sidebar-ov-state-item sidebar-ov-state-item--posts" href="/blog/archives/"><div class="sidebar-ov-state-item__count">48</div><div class="sidebar-ov-state-item__name">归档</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--categories" href="/blog/categories/"><div class="sidebar-ov-state-item__count">19</div><div class="sidebar-ov-state-item__name">分类</div></a><a class="sidebar-ov-state-item sidebar-ov-state-item--tags" href="/blog/tags/"><div class="sidebar-ov-state-item__count">20</div><div class="sidebar-ov-state-item__name">标签</div></a></div><div class="sidebar-ov-cc"><a href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.en" target="_blank" rel="noopener" data-popover="知识共享许可协议" data-popover-pos="up"><img src="/blog/images/cc-by-nc-sa.svg"></a></div></section><div class="sidebar-reading"><div class="sidebar-reading-info"><span class="sidebar-reading-info__text">你已阅读了 </span><span class="sidebar-reading-info__num">0</span><span class="sidebar-reading-info__perc">%</span></div><div class="sidebar-reading-line"></div></div></aside></div><div class="clearfix"></div></div></main><footer class="footer" id="footer"><div class="footer-inner"><div><span>Copyright © 2021</span><span class="footer__icon"><i class="fas fa-heart"></i></span><span>何水浪</span></div><div><span>由 <a href="http://hexo.io/" title="Hexo" target="_blank" rel="noopener">Hexo</a> 强力驱动</span><span> v5.4.0</span><span class="footer__devider">|</span><span>主题 - <a href="https://github.com/liuyib/hexo-theme-stun/" title="Stun" target="_blank" rel="noopener">Stun</a></span><span> v2.6.2</span></div></div></footer><div class="loading-bar" id="loading-bar"><div class="loading-bar__progress"></div></div><div class="back2top" id="back2top"><span class="back2top__icon"><i class="fas fa-rocket"></i></span></div></div><div class="search-mask"></div><div class="search-popup"><span class="search-close"></span><div class="search-input"><input placeholder="搜索文章（支持多关键词，请用空格分隔）"></div><div class="search-results"></div></div><script src="https://cdn.jsdelivr.net/npm/jquery@v3.4.1/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@1.5.2/velocity.ui.min.js"></script><script>function initSearch() {
  var isXML = true;
  var search_path = 'search.json';

  if (!search_path) {
    search_path = 'search.xml';
  } else if (/json$/i.test(search_path)) {
    isXML = false;
  }

  var path = '/blog/' + search_path;
  $.ajax({
    url: path,
    dataType: isXML ? 'xml' : 'json',
    async: true,
    success: function (res) {
      var datas = isXML ? $('entry', res).map(function () {
        // 将 XML 转为 JSON
        return {
          title: $('title', this).text(),
          content: $('content', this).text(),
          url: $('url', this).text()
        };
      }).get() : res;
      var $input = $('.search-input input');
      var $result = $('.search-results');
      // 搜索对象（标题、内容）的权重，影响显示顺序
      var WEIGHT = { title: 100, content: 1 };
      var searchPost = function () {
        var searchText = $input.val().toLowerCase().trim();
        // 根据空白字符分隔关键字
        var keywords = searchText.split(/[\s]+/);
        // 搜索结果
        var matchPosts = [];

        // 有多个关键字时，将原文字整个保存下来
        if (keywords.length > 1) {
          keywords.push(searchText);
        }
        // 防止未输入字符时搜索
        if (searchText.length > 0) {
          datas.forEach(function (data) {
            var isMatch  = false;
            // 没有标题的文章使用预设的 i18n 变量代替
            var title = (data.title && data.title.trim()) || '[ 文章无标题 ]';
            var titleLower = title && title.toLowerCase();
            // 删除 HTML 标签 和 所有空白字符
            var content = data.content && data.content.replace(/<[^>]+>/g, '');
            var contentLower = content && content.toLowerCase();
            // 删除重复的 /
            var postURL = data.url && decodeURI(data.url).replace(/\/{2,}/g, '/');
            // 标题中匹配到的关键词
            var titleHitSlice = [];
            // 内容中匹配到的关键词
            var contentHitSlice = [];

            keywords.forEach(function (keyword) {
              /**
              * 获取匹配的关键词的索引
              * @param {String} keyword 要匹配的关键字
              * @param {String} text 原文字
              * @param {Boolean} caseSensitive 是否区分大小写
              * @param {Number} weight 匹配对象的权重。权重大的优先显示
              * @return {Array}
              */
              function getIndexByword (word, text, caseSensitive, weight) {
                if (!word || !text) {
                  return [];
                };

                var startIndex = 0; // 每次匹配的开始索引
                var index = -1;     // 匹配到的索引值
                var result = [];    // 匹配结果

                if (!caseSensitive) {
                  word = word.toLowerCase();
                  text = text.toLowerCase();
                }

                while((index = text.indexOf(word, startIndex)) !== -1) {
                  var hasMatch = false;
                  // 索引位置相同的关键词，保留长度较长的
                  titleHitSlice.forEach(function (hit) {
                    if (hit.index === index && hit.word.length < word.length) {
                      hit.word = word;
                      hasMatch = true;
                    }
                  });
                  startIndex = index + word.length;
                  !hasMatch && result.push({ index: index, word: word, weight: weight });
                }
                return result;
              }
              titleHitSlice = titleHitSlice.concat(getIndexByword(keyword, titleLower, false, WEIGHT.title));
              contentHitSlice = contentHitSlice.concat(getIndexByword(keyword, contentLower, false, WEIGHT.content));
            });

            var hitTitle = titleHitSlice.length;
            var hitContent = contentHitSlice.length;

            if (hitTitle > 0 || hitContent > 0) {
              isMatch = true;
            }
            if (isMatch) {
              ;[titleHitSlice, contentHitSlice].forEach(function (hit) {
                // 按照匹配文字的索引的递增顺序排序
                hit.sort(function (left, right) {
                  return left.index - right.index;
                });
              });
              /**
              * 给文本中匹配到的关键词添加标记，从而进行高亮显示
              * @param {String} text 原文本
              * @param {Array} hitSlice 匹配项的索引信息
              * @param {Number} start 开始索引
              * @param {Number} end 结束索引
              * @return {String}
              */
              function highlightKeyword (text, hitSlice, start, end) {
                if (!text || !hitSlice || !hitSlice.length) {
                  return;
                }

                var result = '';
                var startIndex = start;
                var endIndex = end;
                hitSlice.forEach(function (hit) {
                  if (hit.index < startIndex) {
                    return;
                  }

                  var hitWordEnd = hit.index + hit.word.length;
                  result += text.slice(startIndex, hit.index);
                  result += '<b>' + text.slice(hit.index, hitWordEnd) + '</b>';
                  startIndex = hitWordEnd;
                });
                result += text.slice(startIndex, endIndex);
                return result;
              }

              var postData = {};
              // 文章总的搜索权重
              var postWeight = titleHitSlice.length * WEIGHT.title + contentHitSlice.length * WEIGHT.content;
              // 标记匹配关键词后的标题
              var postTitle = highlightKeyword(title, titleHitSlice, 0, title.length) || title;
              // 标记匹配关键词后的内容
              var postContent;
              // 显示内容的长度
              var SHOW_WORD_LENGTH = 200;
              // 命中关键词前的字符显示长度
              var SHOW_WORD_FRONT_LENGTH = 20;
              var SHOW_WORD_END_LENGTH = SHOW_WORD_LENGTH - SHOW_WORD_FRONT_LENGTH;

              // 截取匹配的第一个字符，前后共 200 个字符来显示
              if (contentHitSlice.length > 0) {
                var firstIndex = contentHitSlice[0].index;
                var start = firstIndex > SHOW_WORD_FRONT_LENGTH ? firstIndex - SHOW_WORD_FRONT_LENGTH : 0;
                var end = firstIndex + SHOW_WORD_END_LENGTH;
                postContent = highlightKeyword(content, contentHitSlice, start, end);
              } else { // 未匹配到内容，直接截取前 200 个字符来显示
                postContent = content.slice(0, SHOW_WORD_LENGTH);
              }
              postData.title = postTitle;
              postData.content = postContent;
              postData.url = postURL;
              postData.weight = postWeight;
              matchPosts.push(postData);
            }
          });
        }

        var resultInnerHtml = '';
        if (matchPosts.length) {
          // 按权重递增的顺序排序，使权重大的优先显示
          matchPosts.sort(function (left, right) {
            return right.weight - left.weight;
          });
          resultInnerHtml += '<ul>';
          matchPosts.forEach(function (post) {
            resultInnerHtml += '<li><a class="search-results-title" href="' + post.url + '">';
            resultInnerHtml += post.title;
            resultInnerHtml += '</a><div class="search-results-content">';
            resultInnerHtml += post.content;
            resultInnerHtml += '</div></li>';
          });
          resultInnerHtml += '</ul>';
        } else {
          resultInnerHtml += '<div class="search-results-none"><i class="far fa-meh"></i></div>';
        }
        $result.html(resultInnerHtml);
      };
      $input.on('input', searchPost);
      $input.on('keyup', function (e) {
        if (e.keyCode === Stun.utils.codeToKeyCode('Enter')) {
          searchPost();
        }
      });
    }
  });
}

function closeSearch () {
  $('body').css({ overflow: 'auto' });
  $('.search-popup').css({ display: 'none' });
  $('.search-mask').css({ display: 'none' });
}

window.addEventListener('DOMContentLoaded', function () {
  Stun.utils.pjaxReloadLocalSearch = function () {
    $('.header-nav-search').on('click', function (e) {
      e.stopPropagation();
      $('body').css('overflow', 'hidden');
      $('.search-popup')
        .velocity('stop')
        .velocity('transition.expandIn', {
          duration: 300,
          complete: function () {
            $('.search-popup input').focus();
          }
        });
      $('.search-mask')
        .velocity('stop')
        .velocity('transition.fadeIn', {
          duration: 300
        });

      initSearch();
    });
    $('.search-mask, .search-close').on('click', function () {
      closeSearch();
    });
    $(document).on('keydown', function (e) {
      // Escape <=> 27
      if (e.keyCode === Stun.utils.codeToKeyCode('Escape')) {
        closeSearch();
      }
    });
  };

  Stun.utils.pjaxReloadLocalSearch();
}, false);

function safeOpenUrl(url) {
  var newTab = window.open();
  newTab.opener = null;
  newTab.location = url;
}

function extSearch(engine) {
  var engines = {
    google: 'https://www.google.com/search?q=',
    bing: 'https://cn.bing.com/search?q=',
    baidu: 'https://www.baidu.com/s?ie=UTF-8&wd=',
  };
  var host = window.location.host;
  var query = $('.search-input input').val().toLowerCase().trim();
  var uri = engines[engine] + query + ' site:' + host;

  if (query) {
    safeOpenUrl(uri);
  } else {
    Stun.utils.popAlert('warning', '请输入字符');
  }
}

var assistSearchList = window.CONFIG.assistSearch;

if (Array.isArray(assistSearchList)) {
  assistSearchList.forEach(function (name) {
    document.querySelector('.search-btns-item--' + name).addEventListener('click', function () {
      extSearch(name);
    }, false);
  });
}</script><script src="https://cdn.jsdelivr.net/npm/leancloud-storage@latest/dist/av-min.js"></script><script src="https://cdn.jsdelivr.net/npm/valine@latest/dist/Valine.min.js"></script><script>function loadValine () {
  var GUEST_INFO = ['nick', 'mail', 'link'];
  var guest_info = 'nick';

  guest_info = guest_info.split(',').filter(function(item) {
    return GUEST_INFO.indexOf(item) > -1;
  });
  new Valine({
    el: '#valine-container',
    appId: 'fJ4fFnieffDVKy4Jkn16aHdO-gzGzoHsz',
    appKey: 'qfVo2CUC2pJ3udFzLFnd0GBM',
    notify: true,
    verify: true,
    placeholder: '说点啥再走呗~爱你',
    avatar: 'mp',
    meta: guest_info,
    pageSize: '10' || 10,
    visitor: false,
    recordIP: false,
    lang: '' || 'zh-cn',
    path: window.location.pathname
  });
}

if (false) {
  loadValine();
} else {
  window.addEventListener('DOMContentLoaded', loadValine, false);
}</script><script src="/blog/js/utils.js?v=2.6.2"></script><script src="/blog/js/stun-boot.js?v=2.6.2"></script><script src="/blog/js/scroll.js?v=2.6.2"></script><script src="/blog/js/header.js?v=2.6.2"></script><script src="/blog/js/sidebar.js?v=2.6.2"></script><script type="application/json" src="/blog/search.json"></script></body></html>